问题描述
这很好(无限循环):
$ while TRUE; do printf ".";done
............................................... .....................
.............................................................................
我正在尝试使用timeout
命令超时此while loop
.所有这些都不起作用:
I am trying to timeout this while loop
with the timeout
command.All these don't work:
$ 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')
正确的方法是什么(如果存在)?
What is the correct way (if it exists)?
推荐答案
我认为,解决您的问题的方法是执行另一个Shell实例并将适当的命令传递给它.根据bash手册:
I think that the solution to your problem is to execute another shell instance and pass proper commands to it.According to bash manual:
-c If the -c option is present, then commands are read from the first non-option argument command_string.
因此,我的解决方案将是这样的:
Thus my solution would be something like that:
timeout 5 bash -c -- 'while true; do printf ".";done'
--
确保将以下参数视为非选项.而''
有助于通过"
而不造成不必要的转义
--
assures that the following arguments will be treated as non-option. And ''
helps with passing "
without unnecessary escaping
这篇关于Linux Shell脚本中的while循环超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!