向bash中的while循环添加超时命令时出错

向bash中的while循环添加超时命令时出错,bash,timeout,command,Bash,Timeout,Command,因此,我的while循环工作正常,但当我添加timeout命令时,会出现以下错误: 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 Proc

因此,我的while循环工作正常,但当我添加timeout命令时,会出现以下错误:

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
[
$?
=
0
]
执行命令。然后(由于
)您想要执行
do…

当bash解析您的行时,它会抱怨
do
关键字不在这里的合法构造中……这是出乎意料的

要快速修复此问题,请将命令包装如下:

timeout 30s bash -c 'while ....'