所以我的 while 循环工作正常,但是当我添加超时命令时,它给了我这个错误:
bash: syntax error near unexpected token `do'
这是命令:
timeout 30s while [ $? = 0 ]; do kill -0 $MYPID 2>/dev/null; if [ $? = 0 ]; then echo The Process PID is running && date +%r; else echo the Process PID is NOT running && date +%r; fi; done
最佳答案
Bash 看到这一点:您想要使用参数 30s
、while
、[
执行命令 timeout
, $?
、=
、0
和 ]
。然后(因为 ;
)您想要执行 do ...
。
当 bash 解析您的行时,它会提示 do
关键字不在此处的合法构造中......这是意外的!
要快速修复,请像这样包装命令:
timeout 30s bash -c 'while ....'
关于bash - 在 bash 中的 while 循环中添加超时命令时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26664882/