2014-10-30 51 views
1

所以我while循环工作正常,但是当我添加了timeout命令它给我这个错误:错误添加超时命令时,while循环在bash

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 
+1

你执行命令'timeout'带参数的'30s','while','[','$','? =','0'和']'。然后你想执行'do ...'。当bash分析你的线路时,它会抱怨这个“do”在这里没有合法的结构......这是意想不到的! – 2014-10-30 23:06:12

+2

你可以像这样包装所有东西:'timeout 30s bash -c'while ....''。 – 2014-10-30 23:08:27

+0

非常感谢!这解决了它。 – the1osu 2014-10-30 23:12:11

回答

0

击看到这个:你想带参数30swhile[$?=0和要执行的命令。然后(因为;)你想执行do ...

当bash分析你的线路时,它会抱怨do关键字不在这里的合法结构中......这是意想不到的!

快速修复,换你的命令,如下所示:

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