linux - 在 Linux shell 脚本中超时 while 循环

标签 linux loops timeout

这很好用(无限循环):

$ 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/

相关文章:

Node.JS:不保持进程运行的setTimeout

linux - ElasticSearch 中的超时更改不起作用

tomcat - 使用 CARGO 在 tomcat 上远程部署失败并超时

linux - 在 Linux 中打印特定文本行的单个部分

linux - Git 不会通过 cifs mount 重置文件权限

php - 如何为 PHP 安装 OpenCV?

linux - 运行相同操作系统问题的两个不同大小的图像文件的完整性

R:矩阵中的子集只有那些在特定列中具有特定值的行

PHP while循环,表格行

Python np.asarray 不返回真实形状