How can you run a command in bash over until success

J V picture J V · Mar 11, 2011 · Viewed 141.9k times · Source

I have a script and want to ask the user for some information, the script cannot continue until the user fills in this information. The following is my attempt at putting a command into a loop to achieve this but it doesn't work for some reason.

echo "Please change password"
while passwd
do
echo "Try again"
done

I have tried many variations of the while loop:

while `passwd`
while [[ "`passwd`" -gt 0 ]]
while [ `passwd` -ne 0 ]]
# ... And much more

But I can't seem to get it to work.

Answer

Erik picture Erik · Mar 11, 2011
until passwd
do
  echo "Try again"
done

or

while ! passwd
do
  echo "Try again"
done