I have a question about a while loop. I have been searching and I can't find anything that will assist. I might be missing something. I am a little new with shell. I am trying to verify the user password to make sure that it is correct. I was able to get part of it but the loop exits out once the user inputs their password. I want to loop to continue until it is complete. Below I have attached what I do have. I know that I am missing something, I am just not sure. I was able to create an infinite loop but I wasn't able to figure the rest out.
read -p "Input new username please?: " user
read -p "Input password: " password
read -p "Re-enter password: " verify
if [ "$password" != "$verify" ]
then
echo "Password does not match. Please try again."
else
echo "Password Successful!!"
fi
CodePudding user response:
If you are still stuck, continuing from the comment, what you want to do is simply wrap your code in a continual loop and provide a break
statement when the passwords match. A convenient form of a continual loop is:
while :
do
## your code goes here
done
The ':'
operator tests true
each iteration.
You can rearrange your if
statement as if [ "$password" = "$verify" ]
and then output success and break
to terminate (e.g. jump out of the loop) at that point. There is no need for the else
part. If the passwords don't match, just output the error message and loop again.
Another improvement would be to use printf
instead of echo
(in all cases). The printf
function is far superior and provides the same output format control as man 3 printf for C
Putting it altogether, you could do:
#!/bin/sh
while : ## loop continually
do
read -p "Input username : " user
read -p "Input password : " password
read -p "Re-enter password : " verify
if [ "$password" = "$verify" ] ## check for match
then
printf "\nPassword Successful!!\n"
break;
fi
## otherwise loop again
printf "\nerror: Password does not match. Please try again.\n\n" >&2
done
Example Use/Output
$ sh pwchk.sh
Input username : myUser
Input password : myGoodPass
Re-enter password : myBadPass
error: Password does not match. Please try again.
Input username : myUser
Input password : myGoodPass
Re-enter password : myGoodPass
Password Successful!!
Look things over and let me know if you have further questions.
CodePudding user response:
You don't have a loop, but you can add one with something like:
#!/bin/sh
read -p "Input new username please?: " user
while
read -p "Input password: " password
read -p "Re-enter password: " verify
test "$password" != "$verify"
do
echo "Password does not match. Please try again." >&2
done
That gets a little sloppy about handling EOF, so you might want to check the reads with:
#!/bin/sh
read -p "Input new username please?: " user || exit
while
read -p "Input password: " password \
&& read -p "Re-enter password: " verify \
&& test "$password" != "$verify"
do
echo "Password does not match. Please try again." >&2
done
You might want to suppress the printing of the password as it is being typed, which you can do with:
#!/bin/sh
trap 'stty echo' EXIT
read -p "Input new username please?: " user || exit
while
stty -echo echonl
read -p "Input password: " password \
&& read -p "Re-enter password: " verify \
&& test "$password" != "$verify"
do
echo "Password does not match. Please try again." >&2
done