Home > front end >  How to make a for loop that kicks user after 3 failed username and passsword attempts?
How to make a for loop that kicks user after 3 failed username and passsword attempts?

Time:06-08

I am new to Bash Script. I am having trouble making a for loop that kicks the user after 3 failed attempts for typing the wrong username and/or password. I have been looking at other websites and trying them, but nothing seems to work. This is what I have so far Help

#!/bin/bash"
    while true; do
        read -p "Please Enter Username  : " Admin
        read -p "Please Enter Username again : " Username
        if [[ "$Username" == "$Admin" ]]
        then
            echo "Username is Correct"
            break;
        else 
        printf "Username is Wrong" 
        fi
    done
    while true; do
    echo -n "Enter a password: " 
    read -s firstpassword
    echo
    read -p "Retype a password: " secondpassword
    echo
    if [ $firstpassword == $secondpassword ];
    then
      echo "Password is correct." 
    break;
    else
    printf " Password is wrong." 
    fi
    done

CodePudding user response:

#!/bin/bash
while true; do
    read -p "Please Enter Username: " Admin
        read -p "Please Enter Username again: " Username
        if [[ "$Username" == "$Admin" ]]
        then
            echo "Username is Correct"
            break;
        else 
        printf "Username is Wrong\n"
    fi
done
password="1234"
n=0
function login() {
n=$((n 1))
if (( $n > 3 ))
then
    echo "To many attempts, terminating.."
    return 0
else

    echo -n "Enter the password: "
    read pwd
    if [ $password = $pwd ]
    then
        echo "Password is correct"
    else
        echo "Password is incorrect!"
        login
    fi
fi
}
login
  • Related