bash shell while loop无法break


为什么这个while循环执行完echo while00 && echo while01后不break

while $(sleep 0.5); do pgrep -f baidu > /dev/null 2>&1 || (echo while00 && echo while01 && break); done

监听进程退出后,执行后面的命令
比如ping -c 10 -i 1 baidu.com

Linux shell bash

SunnY耀 9 years, 7 months ago

(...) 会创建一个子 shell,所以 break 不工作。

其实按照你的代码意图,这里不写括号是完全没问题的,直接去掉括号就可以了。

P.S. 你前面写的 while $(sleep 0.5) 是对 shell 命令的误用,这里应该写 while sleep 0.5 ,加个 $ 多此一举了。

秒速⑤厘米 answered 9 years, 7 months ago

Your Answer