stop()
{
sh stop.sh
ret_code=$?
<1>echo ret_code is $ret_code
}
once=true
while $once
do
stop & PID=$!
<2>echo ret_code is $ret_code
sleep 2m
<3>echo ret_code is $ret_code
if [ $ret_code == 0 ]
then
break
else
kill $PID
fi
done
在&lt; 1&gt;处的ret_code。是0
在&lt; 2&gt;处的ret_code不打印
在&lt; 3&gt;处的ret_code。是空白的
有人可以告诉为什么在sleep命令期间和之后更改了ret_code值吗?
编辑:
我尝试了另一种方法。它奏效了 -
#! /bin/sh
once=true
while $once
do
sh stop.sh & PID=$!
sleep 2m
kill $PID
if [ $? != 0 ]
then
echo stop has completed successfully within the time limit
echo starting ...
sh start.sh
break
fi
done
答案 0 :(得分:1)
stop
作为后台流程在子shell中运行,因此它设置的ret_code
的值与您在其中检查的值不同&lt; 2&gt;和&lt; 3&gt; (它应该是相同的,因为它的值不受sleep
命令的影响)。