1

I'm trying to test if an account is locked out, using a value of '1' to set a lock, and if not allow a user to log in. (This is just a silly script to learn bash with, not a real login, so ignore any security flaws!)

After 3 failed attempts it should set the flag to 1 then exit the script. However, when running the script a second time it sets it to the default of 0 again instead of failing to run due to the flag.

I suspect the problem is due to me defaulting the flag variable to 0 in order to avoid errors about an uninitialised variable, but I have no idea how to make it "remember" the variable was set to 1 for each instance the script runs.

let x=0
let attempts=0
let PinCode=1234

#checks if locked, default variable value set to 0 (so it is initiated even if first time running script)
if [ ${locked:-0} -eq 1 ]

then
        echo "You are locked out of your account."
else

#runs if unlocked, until third attempt or successful entry
until [ $attempts -eq 3 -o $PinCode -eq $x ]
  do
        echo "Enter your PIN number"
        read x
        if [ $x -eq $PinCode ]

                then
                        echo "Welcome $USER you have correctly entered your PIN. You are connecting from $SSH_CLIENT at $HOSTNAME."

                else
                        let attempts=attempts+1
                        echo -e "Incorrect PIN number"
                        echo -e "\nYou have entered your pin number incorrectly $attempts times. At 3 failures you will be locked out of your
account!"

#locks account on third attempt
                        if [ $attempts -eq 3 ]
                                then let locked=1
                                fi
        fi

  done

fi

exit 0

Any help is very appreciated!

2 Answers 2

1

I fixed it by using a file called account.locked as my flag, creating it with "touch" and checking for its existance with if [ -f filename ]

let x=0
let attempts=0
let PinCode=1234

#checks if locked by searching for flag file
if [ -f locked.account ]
then
        echo "You are locked out of your account."
else

#runs if unlocked, until third attempt or successful entry
until [ $attempts -eq 3 -o $PinCode -eq $x ]
  do
        echo "Enter your PIN number"
        read x
        if [ $x -eq $PinCode ]

                then
                        echo "Welcome $USER you have correctly entered your PIN. You are connecting from $SSH_CLIENT at $HOSTNAME."

                else
                        let attempts=attempts+1
                        echo -e "Incorrect PIN number"
                        echo -e "\nYou have entered your pin number incorrectly $attempts times. At 3 failures you will be locked out of your
account!"

#locks account on third attempt by creating a flag file as a type of global variable replacement
                        if [ $attempts -eq 3 ]
                                then touch locked.account
                                echo "You are locked out of your account."
                                fi
        fi

  done

fi

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

Comments

0

Launching the script from terminal makes the script inherit the environment variables from your terminal. Any environment variables set within that script are only valid during the execution time of the same script. As soon as it exits, away goes the environment variables.

You would have to use something else, files for instance, to keep track of changes during multiple executions of the script.

Comments

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.