Help with bash for loop
by woodson2 from LinuxQuestions.org on (#598NR)
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.
Code: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


Code: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