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!