bash - 在 bash 中的 while 循环中添加超时命令时出错

标签 bash timeout command

所以我的 while 循环工作正常,但是当我添加超时命令时,它给了我这个错误:

bash: syntax error near unexpected token `do'

这是命令:

timeout 30s while [ $? = 0 ]; do kill -0 $MYPID 2>/dev/null; if [ $? = 0 ]; then echo The Process PID is running && date +%r; else echo the Process PID is NOT running && date +%r; fi; done

最佳答案

Bash 看到这一点:您想要使用参数 30swhile[ 执行命令 timeout$?=0]。然后(因为 ;)您想要执行 do ...

当 bash 解析您的行时,它会提示 do 关键字不在此处的合法构造中......这是意外的!

要快速修复,请像这样包装命令:

timeout 30s bash -c 'while ....'

关于bash - 在 bash 中的 while 循环中添加超时命令时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26664882/

相关文章:

Bash 使用 codegangsta cli 框架自动完成 [https ://github. com/codegangsta/cli]

bash - 将数字转换为人类可读的 KB/MB/GB/TB

javascript - JScript 超时触发器 <img>

python - pytest-timeout - 测试失败而不是杀死整个测试运行

command - cmake自定义命令以复制和重命名

bash - 为进程分配 cpu 核心 - Linux

bash - 让 Ctrl-p 和 Ctrl-n 搜索按键入字符过滤的 shell 历史记录

mysql - SQL通过电子邮件字段获取一次性客户

macos - Mdfind 排除文件夹

python - 如何在 Windows 命令提示符下运行 python 文件?