This question already has answers here:
How do I set a variable to the output of a command in Bash?
(14个答案)
unary operator expected
(4个答案)
去年关门了。
我试图编写一个简单的脚本,如果一行有7个逗号,则返回true或false。但我遇到了几个错误。第一个错误来自grep,如果删除-o,我仍然会收到错误。我不确定为什么显示0,为什么返回为false?我也很难使用equal;因为-eq或==都不起作用。
脚本:
#!/bin/bash
str="a,g,5,d,s,c,f,s"
stat=grep -o "," <<< "$str" | wc -l
if [ $stat == '7' ];then
        echo "true"
else
        echo "false"
fi

这是我收到的输出:
./tesh.sh
./tesh.sh: line 3: -0: command not found
0
./tesh.sh: line 4: [: ==: unary operator expected
false

最佳答案

请更正脚本中的以下行。

stat=$(grep -o "," <<< "$str" | wc -l)

在与数值进行比较时,如果出现如下情况,也请更正
if [[ $stat -eq 7 ]];then

关于linux - 简单脚本无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51385548/

10-11 22:37
查看更多