【问题标题】:remember bash variable for running script again记住再次运行脚本的 bash 变量
【发布时间】: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


    【解决方案1】:

    我通过使用一个名为 account.locked 的文件作为我的标志来修复它,使用“touch”创建它并使用 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
    

    【讨论】:

      【解决方案2】:

      从终端启动脚本会使脚本从终端继承环境变量。该脚本中设置的任何环境变量仅在同一脚本的执行期间有效。一旦退出,环境变量就会消失。

      您必须使用其他东西(例如文件)来跟踪脚本多次执行期间的更改。

      【讨论】:

        猜你喜欢
        • 2014-02-19
        • 1970-01-01
        • 2020-09-09
        • 1970-01-01
        • 1970-01-01
        • 2016-01-22
        • 2013-08-20
        • 2017-05-22
        相关资源
        最近更新 更多