How to run a command repeatedly until it succeeds:
passwd
while [ $? -ne 0 ]; do !!; done
The !! is a shortcut to the last command run. Run the command once and then the while loop will execute until the command succeeds (i.e. returns 0)
If you’re serious about Bash scripting - check one of these books out: