【发布时间】:2013-12-17 13:09:14
【问题描述】:
我正在尝试测试一个帐户是否被锁定,使用值 '1' 设置锁定,如果不允许用户登录。(这只是一个学习 bash 的愚蠢脚本,不是真正的登录,因此请忽略任何安全漏洞!)
在 3 次尝试失败后,它应该将标志设置为 1,然后退出脚本。但是,当第二次运行脚本时,它会再次将其设置为默认值 0,而不是由于标志而无法运行。
我怀疑问题是由于我将标志变量默认为 0 以避免有关未初始化变量的错误,但我不知道如何让它“记住”脚本的每个实例的变量设置为 1运行。
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
非常感谢任何帮助!
【问题讨论】:
标签: bash shell variables scripting boolean