Shell命令运行时出错

时间:2017-08-03 12:21:00

标签: shell command

当我运行以下命令时,我得到输出,但它会抛出

  找不到

命令

错误。我不知道为什么会出现这个错误。有什么建议吗?

val1 = ps -ef|grep -i active | wc -l
val2 = 2

echo $val1

if [$val1 -ge $val2]  ; then
        echo "Active MQ process is Running on $HOSTNAME and the process total $(netstat -a | grep 61614 | wc -l)"
else
        echo "Active MQ process appears DOWN $HOSTNAM"
fi

下面的输出,

 bash-4.1# ./testact.sh
./testact.sh: line 4: val1: command not found
0
./testact.sh: line 5: val2: command not found

Active MQ process is Running on r00du3n0c and the process total 524
bash-4.1#

1 个答案:

答案 0 :(得分:1)

通过ShellCheck运行您的脚本,您将收到此错误:

Line 6:
if [$val1 -ge $val2]  ; then
^-- SC1009: The mentioned parser error was in this if expression.
   ^-- SC1035: You need a space after the [ and before the ].
   ^-- SC1073: Couldn't parse this test expression.
                    ^-- SC1020: You need a space before the ].
                    ^-- SC1072: Missing space before ]. Fix any mentioned problems and try again.

通过添加空格来解决此问题:

if [ $val1 -ge $val2 ]  ; then

然后你会得到更多错误:

Line 1:
val1 = ps -ef|grep -i active | wc -l
^-- SC2037: To assign the output of a command, use var=$(cmd) .
^-- SC2148: Tips depend on target shell and yours is unknown. Add a shebang.
     ^-- SC1068: Don't put spaces around the = in assignments.
              ^-- SC2126: Consider using grep -c instead of grep|wc -l.

Line 2:
val2 = 2
     ^-- SC1068: Don't put spaces around the = in assignments.

如上所示,删除空格并修复命令输出分配。

val1=$(ps -ef | grep -i active | wc -l)
val2=2