Help with bash for loop

The code below works just fine as long as the exit status equals 0, however I’d like the script to exit after 3 times if the exit status is non-zero. Right now the script continues on even if the exit status is non-zero after the 3 iterations.

for run in {1..3}
do
kinit $USER
   if [ $? -eq 0 ]; then
   echo ""
    echo -e "${bldgrn}Good. Now lets us continue${txtrst}"
     echo ""
      sleep .5
       break
   else
    sleep 1
    echo -e "${bldred}Please verify your credentials and try again!${txtrst}"
   fi
done

Just add a line before the for end.

    for run in {1..3}
    do
        kinit $USER
    if [ $? -eq 0 ]; then
        echo “”
        echo -e “${bldgrn}Good. Now lets us continue${txtrst}”
        echo “”
        sleep .5
        break
    else
        sleep 1
        echo -e “${bldred}Please verify your credentials and try again!${txtrst}”
    fi
    #---------------------------------------------------------
    [[ $run -eq 3 ]] && echo "leaving" && exit 999
    #---------------------------------------------------------
    done

Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki