Home  >  Q&A  >  body text

linux - 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

黄舟黄舟2742 days ago715

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-17 12:03:12

    (...) creates a subshell, so break doesn't work.

    In fact, according to the intention of your code, it is completely fine not to write brackets here. Just remove the brackets.

    P.S. The while $(sleep 0.5) you wrote earlier is a misuse of the shell command. You should write while sleep 0.5 here, and adding a $ is unnecessary.

    reply
    0
  • Cancelreply