One way would be to put the code into a function, call it and then use its return value to determine the next course of action:
Bash:
function dosomething()
{
for run in {1..3}
do
if kinit "$USER"; then
echo ""
echo -e "${bldgrn}Good. Now lets us continue${txtrst}"
echo ""
sleep .5
return 0
else
sleep 1
echo -e "${bldred}Please verify your credentials and try again!${txtrst}"
fi
done
# if we get here - the function failed three times
return 1
}
if dosomething; then
echo "Function succeeded!!"
# do whatever else you need to do here
exit 0
else
echo "Function failed!!"
# do whatever you need to do here
exit 1
fi
NOTE:
Instead of calling the kinit command and check it's return value using $? - a better way to do it is just to check the return status directly in the if statement.
e.g.
The loop inside the function will run a maximum of three times.
If kinit returns 0 (success) - the function will return 0
If kinit returns anything else (failure) - the function will loop again.
Once the loop has done three iterations, the function will return 1 (failure)
Then in the main body of the script we use:
to run the function and check it's return status/value and act appropriately.
That's one way of dealing with it. But there are probably several other approaches that would work just as well!
Also - I don't know exactly what you're doing here - so I just called the function dosomething - you might want to give it a more descriptive name!