I have a loop that will prompt for the sudo
password until it gets it correct. The why isn’t relevant, lets just say it’s part of a bigger script and other solutions aren’t appropriate.
until sudo -n true 2> /dev/null; do
read -s -p "Password:" sudo_password
sudo -S -v <<< "${sudo_password}" 2> /dev/null
done
This works very well. However, it does not work when inside brackets
until [[ $(sudo -n true 2> /dev/null) ]]; do
read -s -p "Password:" sudo_password
sudo -S -v <<< "${sudo_password}" 2> /dev/null
done
I’ve tried this in a myriad of ways, with both loops and conditionals. I even went so far as doing
until [[ $(sudo -n true 2> /dev/null; echo "$?") -ne 0 ]]; do
read -s -p "Password:" sudo_password
sudo -S -v <<< "${sudo_password}" 2> /dev/null
# check exit status is indeed 0
sudo -n true 2> /dev/null
echo "$?"
done
This also does not work, and I do not understand why. Actually, this case always returns 0
(according to the last echo "$?"
) even if the password is wrong, but it never jumps out of the loop, even if the password is correct, which is even weirder.
Edit: Just realised why the last one didn’t work, while it seemed to in other cases. With that particular comparison, it should’ve been while
, not until
.