353

I have a script and want to ask the user for some information, but 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.

7 Answers 7

559
until passwd
do
  echo "Try again"
done

or

while ! passwd
do
  echo "Try again"
done
8
  • 79
    oneliner: until passwd; do echo "Try again"; done
    – tig
    Mar 19, 2012 at 12:10
  • 32
    Difficult to Ctrl-C out of this.
    – DonGar
    Feb 10, 2013 at 22:38
  • 84
    Easy to Ctr-C out of this: until passwd; do echo "Try again"; sleep 2; done - all you have to do is press Ctr-C right after (within the two seconds given) the echo did it's job.
    – Christian
    Aug 23, 2013 at 20:14
  • 47
    Ctrl-Z followed by kill %1 works here when Ctrl-C won't
    – Tom
    Apr 24, 2014 at 17:36
  • 14
    @azmeuk: Try something like until passwd || (( count++ >= 5 )); do echo "foo"; done (bash only, make sure to set count to 0 if that varaible exists) If you need this for plain sh, increment the counter in the body and use [ ] Aug 5, 2015 at 11:53
127

To elaborate on @Marc B's answer,

$ passwd
$ while [ $? -ne 0 ]; do !!; done

Is a nice way of doing the same thing that's not command specific.

If you want to do this as an alias (kudos to @Cyberwiz):

alias rr='while [ $? -ne 0 ]; do eval $(history -p !!); done'

Usage:

$ passwd
$ rr
6
  • 14
    Doesn't work for me unfortunately (I get '!!' command not found). How is it supposed to work? Sep 10, 2014 at 10:05
  • 5
    It is a bash trick to run the previous command. For example if you forget to write sudo in front of a command, you can simply do sudo !! to run the previous command with root privileges.
    – JohnEye
    Jan 8, 2015 at 13:34
  • 3
    How to sleep between runs? Oct 25, 2017 at 17:04
  • 1
    ^^ while [ $? -ne 0 ]; do !!; sleep 1; done Oct 17, 2020 at 6:19
  • 2
    If you want to do this as an alias, with sleep: alias rr='while [ $? -ne 0 ]; do sleep 1; eval $(history -p !!); done'
    – Cyberwiz
    Jan 12, 2023 at 20:54
115

You need to test $? instead, which is the exit status of the previous command. passwd exits with 0 if everything worked ok, and non-zero if the passwd change failed (wrong password, password mismatch, etc...)

passwd
while [ $? -ne 0 ]; do
    passwd
done

With your backtick version, you're comparing passwd's output, which would be stuff like Enter password and confirm password and the like.

6
  • 2
    I like this because it's clear how to adapt it for the opposite situation. e.g. run a program with a non-deterministic bug until it fails
    – Eric
    May 15, 2014 at 23:54
  • When success is indicated by a non-zero exit code, this is what you need. Jan 30, 2017 at 14:19
  • 13
    I think this can be slightly improved upon by changing the first passwd with /bin/false if you have some long and complicated command that you don't want to keep two versions of.
    – coladict
    Jan 3, 2019 at 9:49
  • 1
    Should be the accepted answer imho. This should work in most shells (tested in bash, mksh, and ash) and is easily adaptable to the situation.
    – unixandria
    Apr 20, 2020 at 14:42
  • 2
    Fails shellcheck with output: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. [SC2181]
    – Nicodemuz
    Jul 28, 2020 at 5:18
30

If anyone looking to have retry limit:

max_retry=5
counter=0
until <YOUR_COMMAND>
do
   sleep 1
   [[ counter -eq $max_retry ]] && echo "Failed!" && exit 1
   echo "Trying again. Try #$counter"
   ((counter++))
done
2
  • 4
    $command isn't a great placeholder -- see BashFAQ #50 on why using strings to store commands is innately unreliable. Feb 22, 2020 at 13:18
  • 1
    I think in this example $command should just be replaced by your arbitrary command. Not necessarily used as string. I like to use <YOUR_COMMAND> to indicate user has to replace something here
    – rufreakde
    Mar 9, 2023 at 13:42
10

You can use an infinite loop to achieve this:

while true
do
  read -p "Enter password" passwd
  case "$passwd" in
    <some good condition> ) break;;
  esac
done
1
  • 2
    This is the only answer that didn't require putting my multi-line command in a function. I did && break after my verification command though instead of the select case. Apr 27, 2018 at 21:19
2
while [ -n $(passwd) ]; do
        echo "Try again";
done;
2
  • 7
    This is not the way to do it... you're negating the stdout of psswd and then doing a unary test on it. @duckworthd is good.
    – Ray Foss
    Jun 13, 2018 at 18:20
  • 2
    Moreover, because there isn't quoting, the test is going to misbehave when there is output but it's more than one word, or is a glob expression that matches files in the current directory, or so forth. Feb 21, 2020 at 18:34
0

It becomes a little tricky if you want the strict mode (set -e) and none of above worked.

set -euo pipefail

counter=0
max_attempts=3
while ret=0; <your command> || ret=$?; [ $ret -ne 0 ]
do
  [[ $counter -eq $max_attempts ]] && echo "Command failed"; exit 1
  sleep 2
  echo "Trying again"
  counter=$((counter+1))
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.