如果在bash脚本中执行if-then语句,如下所示,其中:
if [ "$pgrep foo_process" ] > 0; then
other foo
fi当foo_process正在运行时,上面的if-语句应该产生true结果,因为
pgrep foo_process 将大于零。但是在下面的"do while“脚本中,它没有检测到foo_process何时停止。
#!/bin/bash
if [ "$pgrep foo_process" ] > 0; then
while [ "$pgrep foo_process" ] > 0; do
/home/scripts/arttst.sh
sleep 2
done
else
fi
exit 4为什么?
即使使用pgrep语法来获得二进制输出(0或1),它仍然不会工作:
#!/bin/bash
#pgrep foo_process
if [ "$pgrep -f foo_process &> /dev/null ; echo $?" ] = 0; then
while [ "$pgrep -f foo_process &> /dev/null ; echo $?" ] = 0; do
bash /home/script/arttst.sh
sleep 2
done
else
exit 4
fi
exit 4发布于 2017-10-26 07:32:53
使用带-x开关的pgrep解决:
if pgrep -x "foo_process" > /dev/null; then
while pgrep -x "foo_process" > /dev/null; do
bash /home/scripts/arttst.sh
sleep 2
pgrep -x "foo_process" > /dev/null
done
else
fi
exit 4https://stackoverflow.com/questions/46943027
复制相似问题