我有以下shell脚本:
#!/bin/sh
output=`./process_test.sh status_pid | grep "NOT STARTED: process_1" --line-buffered`
if[[ -z ${output} ]]
then
echo "process is not running"
else
echo "process is running"
fi
其中./process_test.sh status_pid
是我的实用程序,用于查找进程是否正在运行。例如。如果process_1
未运行,则会提供:NOT STARTED: process_1
。进一步
这个实用程序是完美的,没有任何问题。我怀疑问题是if
语法
在运行此脚本时,我得到以下输出:
./test.sh: line 18: if[[ -z NOT: command not found
./test.sh: line 19: syntax error near unexpected token `then'
./test.sh: line 19: `then'
您能帮忙解决这个问题吗?
答案 0 :(得分:3)
您必须使用空格将if
等关键字与[[
等参数或命令分开。
#!/bin/sh
output=$(./process_test.sh status_pid | grep -e "NOT STARTED: process_1" --line-buffered)
if [[ -z ${output} ]]
then
echo "process is not running"
else
echo "process is running"
fi
答案 1 :(得分:1)
你应该像
一样写if [[ -z ${output} ]]
then
...
所以你错过了。
答案 2 :(得分:0)
写这个会更清晰:
#!/bin/sh if ! ./process_test.sh status_pid | grep "NOT STARTED: process_1" > /dev/null; then echo "process is not running" else echo "process is running" fi
请注意, - line-buffering参数无关紧要,因为 在所有输入之后,管道才会完成 被读了。 (嗯,这不是完全无关紧要的 - 它会成为 脚本运行速度可以忽略不计。)
还要注意'[['不是标准的。根据{{3}},它
“在一些实施中可能被认为是(a)保留(字)......导致未指明的结果”。换句话说,它通常被称为“bashism”(虽然它在bash之外的shell中有效),如果你使用它,你不能使用#!/bin/sh
作为你的解释器,但是应该指定{{ 1}}。