使用 -z
或 -n
对一个变量判空时,需要注意若直接使用 [ -n ${ARG} ]
这种形式,若${ARG}
中有空格将会报错:
if [ -z $old_run_pid ];then
echo "Process Non-existent !"
else
kill -9 ${old_run_pid}
mid_run_pid=$(ps ax|grep $path/run.sh|grep -v grep|awk '{print $1}')
if [ -z ${mid_run_pid} ];then
echo "Process Close Success !"
else
echo "Process Close Fail !"
exit 1
fi
fi
输出:
stop.sh: 21: [: 31016: unexpected operator
显然不对
解决方法,使用 [[ -n ${ARG} ]]
或 [ -n "${ARG}" ]
eg:
if [ -z "$old_run_pid" ];then
echo "Process Non-existent !"
else
kill -9 ${old_run_pid}
mid_run_pid=$(ps ax|grep $path/run.sh|grep -v grep|awk '{print $1}')
if [ -z "${mid_run_pid}" ];then
echo "Process Close Success !"
else
echo "Process Close Fail !"
exit 1
fi
fi
网友评论