cplane_pid=`pidof hnb_gw.exe`
if [ -z $cplane_pid ]
then
STATUS=`failure`
echo "Cplane hnbgw running $STATUS"
else
STATUS=`success`
echo "Cplane hnbgw running $STATUS"
fi
echo
答案 0 :(得分:4)
如果有hnb_gw.exe
的多个实例,pidof将返回多个pid。 -z
的{{1}}只需要一个pid。一种解决方案可能是使用pidof的[
开关仅返回一个pid。
答案 1 :(得分:4)
您需要Use More Quotes™:
if [ -z "$cplane_pid" ]
在命令之前添加set -x
并在命令之后set +x
显示它的结果。例如:
$ cplane_pid="1 2 3"
$ set -x
$ [ -z $cplane_pid ]
+ '[' -z 1 2 3 ']'
bash: [: too many arguments
换句话说,变量中以空格分隔的值的每个的被用作单个参数。由于-z
只需要一个参数,因此会导致语法错误。
您可以简单地执行
,而不是将其保存为变量if ! pidof hnb_gw.exe > /dev/null
如果该过程不存在,则返回1(“false”)。
答案 2 :(得分:1)
执行时
cplane_pid=`pidof hnb_gw.exe`
然后cplane_pid
可以包含更多(空格分隔)项目。
所以
的扩张if [ -z $cplane_pid ]
将成为
if [ -z firstPid secondPid etc ]
这是您的错误"[: too many arguments"
您可以通过引用变量来解决此问题(您应该在shell中执行此操作)
if [ -z "$cplane_pid" ]
或使用[[
(如果它已安装在您的系统上),这在很多方面都更好。例如,您不需要引用变量:)
if [[ -z $cplane_pid ]]
与
相同if [[ -z "$cplane_pid" ]]
出于测试目的(和像这样的错误)使用-x hasbang bash选项
#!/bin/bash -x
或使用调试部分
-- normal code --
set -x # debug section starts here
[ -z $cplane_pid ] && echo zero
eval something
set +x # debug section ends here
-- normal code --
你也可以调用脚本
/bin/bash -x yourScript.sh
答案 3 :(得分:0)
pidof可以返回多个pid,在这些情况下,你的测试会得到太多的参数。