【问题标题】:Bash : expression recursion level exceeded (error token is ...)Bash:超出表达式递归级别(错误标记是...)
【发布时间】:2015-06-25 15:11:54
【问题描述】:

我正在编写一个程序,打印用户名和用户登录次数,否则打印“未知用户”。

我的代码如下:

iden=$1
c='last | grep -w -c $iden'
if (( $c > 1 ))
then
    echo "$iden $c"
else
    echo "Unknown user"
fi

我不断收到此错误:

-bash: ((: last | grep -w -c 123: 超出表达式递归级别(错误标记为“c 123”)

【问题讨论】:

    标签: linux bash shell unix


    【解决方案1】:

    要将命令的输出存储在变量中,您需要说var=$(command)。因此,使用:

    c=$(last | grep -w -c "$iden")  # always good to quote the variables
    

    而不是

    c='last | grep -w -c $iden'
    

    如果您正在学习 Bash 脚本,将代码粘贴到 ShellCheck 以查看您可能遇到的问题总是很方便。

    【讨论】:

      【解决方案2】:

      你也可以用这个:

      c=`last | grep -w -c "$iden"`
      

      【讨论】:

      猜你喜欢
      • 2019-03-16
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      相关资源
      最近更新 更多