0

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
2
  • 3
    There's no loop in that code... Commented Sep 11, 2021 at 0:00
  • 1
    Use while : ; do put your code here. If the password matches include break and at the end of the loop put a done Commented Sep 11, 2021 at 2:56

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. That did work and I see how that is better than what I was doing. As I am new to shell and still working through a command line book that I purchased, I do have a question you said that printf is superior and does the same thing. Is that something that I should use as good practice? I want to make sure that I have good coding habits
Thank you very much. I also appreciate the information this is going to help me as I move forward with scripting.
0

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

3 Comments

Thank you for pointing that out. That is something that I wasn't sure how to hide the users input when the password is inputted. I see a few different things like, "trap" I have to look that up because I never seen that before and I need to figure that out and see how I can use it.
The trap is a mechanism for doing cleanup. After stty -echo, if you don't invoke stty echo you won't see what you type even after the script ends. You can call stty echo manually, but you need to ensure that you catch all code paths. By putting it in a trap, it will be invoked no matter how the script terminates (unless the script is terminated by a KILL signal). Since you probably have more code after the password prompt, it would make sense to call it directly after the while loop.
Okay, that is good to know. Learn something new everyday.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.