Home > Blockchain >  how to make a verification while loop in shell script
how to make a verification while loop in shell script

Time:06-08

I'd want to ask you a question concerning a while loop. I've looked everywhere and can't find anything that would help. I might be overlooking something. I'm still learning how to use shell. I'm attempting to validate the user password to ensure that it's correct. I was able to grab a portion of it, however the loop ends as the person enters their password. I want the loop to continue until it is finished. What I do have is included below. I'm sure I'm missing something, but I'm not sure what. I was able to build an endless loop, but I couldn't figure out the rest.

#!/bin/bash
# Username and password loop;do
 
        read -p "Please Enter Username  : " Admin
        if [ "$Username" = "$Admin" ]
        then
                echo "Username is Correct"
        else
                echo "Username is Wrong"
 
        fi
        read -p "Please Enter Password  : " Secret
        read -p "Enter Password Again   : " Secret
        if [ "$Password" = "$Secret" ]
        then
                echo "Password is Correct"
        else
                echo "Password is Wrong"
        fi
done

CodePudding user response:

I'm not sure I entirely understand your question, but it seems that you're missing while true; do and the break command:

#!/bin/bash

Admin="the_admin_user"
Secret="the_secret_password"

while true; do
    read -p "Please Enter Username  : " Username
    if [[ "$Username" = "$Admin" ]]
    then
        echo "Username is Correct"
        break
    else
        echo "Username is Wrong"
    fi
done

while true; do
    read -p "Please Enter Password  : " Password1
    read -p "Enter Password Again   : " Password2

    if [[ "$Password1" != "$Password2" ]]
    then
        echo "Passwords don't match! Try again."
    elif [[ "$Password1" = "$Secret" ]]
    then
        echo "Password is Correct"
        break
    else
        echo "Password is Wrong"
    fi
done

echo "Success!"
  • Related