【发布时间】:2012-09-30 17:39:38
【问题描述】:
我的脚本 bash 中有一个奇怪的行为。当 while 条件为真时,脚本行为正确,但如果为假,则根本不执行循环后的命令并且脚本停止。循环后我的命令没有中断。 我看不出问题出在哪里!欢迎任何帮助:) 提前致谢。
while [ expression1 ] || [ expression2 ]
do
echo in the loop
if [ expression3 ] && [ expression4 ] ;
then
commands..
break;
fi
commands..
done
commands..
echo out from the loop
真实代码:
start_t=`grep Start_t $job_template | awk -F= '{print $2}'`
current_date=`date +%s`
progress_t=`expr $current_date - $start_t`
exec_t=`grep Exec_t $job_template | awk -F= '{print $2}'`
running_state="r"
req_state $job_id # get the state
xml_state=` grep "job_id=$job_id" $list_job_file | awk '{print $4}'`
while [ $state = $running_state ] || [ $xml_state = "stoped" ]
do
echo in the loop
if [ "$xml_state" = "running" ] && [ $progress_t -gt $exec_t ] ;
then
kill_job $job_id
update_status $job_template "killed"
echo The job is killed
break;
fi
sleep $sleeping_t
$req_state $job_id # to update the state
echo state $state
xml_state=` grep "job_id=$job_id" $list_job_file | awk '{print $4}' `
echo xml_state $xml_state
start_t=`grep Start_t $job_template | awk -F= '{print $2}'`
current_date=`date +%s`
progress_t=`expr $current_date - $start_t`
done
echo out from the loop
commands..
【问题讨论】:
-
如果您在打印
done后立即执行echo "hi"? -
您应该向我们展示您的真实脚本,而不是伪代码。这里可能缺少一些重要的东西。
-
再次检查您的脚本。重复您的伪代码的脚本绝对可以按预期工作paste.ubuntu.com/1270794
-
@KingsIndian:不,它没有被打印出来。
-
我怀疑 bash 遇到错误并终止,可能带有错误消息。像这样
bash -x <scriptname>运行你的脚本来跟踪它。我怀疑循环中的xml_state最终被设置为空字符串或只有空格,因此[ $xml_state = "stoped" ]表达式变得无效。最好在$xml_state周围加上引号。
标签: bash shell while-loop break