这很好用(无限循环):
$ while TRUE; do printf ".";done
................................................ ...................................
我正在尝试使用 timeout
命令超时此while 循环
。
所有这些都不起作用:
$ timeout 5 while TRUE; do printf ".";done
$ timeout 5 "while TRUE; do printf ".";done"
$ timeout 5 "while TRUE; do printf \".\";done"
$ timeout 5 $(while TRUE; do printf ".";done)
$ timeout 5 $('while TRUE; do printf ".";done')
什么是正确的方法(如果存在)?
最佳答案
我认为您的问题的解决方案是执行另一个 shell 实例并将适当的命令传递给它。 根据 bash 手册:
-c If the -c option is present, then commands are read from the first non-option argument command_string.
因此我的解决方案是这样的:
timeout 5 bash -c -- 'while true; do printf ".";done'
--
确保以下参数将被视为非选项。 ''
有助于传递 "
而无需进行不必要的转义
关于linux - 在 Linux shell 脚本中超时 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27555727/