linux - Bash Shell 执行 While Loop 无限循环?

标签 linux bash shell while-loop do-while

基本上这是我的代码:

bay=$(prog -some flags)
while [ $bay = "Another instance of this program is running, please exit it first" ]
do
echo "Awaiting Access to program"
do
.....

我有一个程序,由于它与我的硬件交互的方式,一次只允许一个实例运行,当另一个实例正在运行时,它会弹出以下消息“该程序的另一个实例正在运行,请退出它首先”。

我需要能够运行多个将使用同一个程序的脚本,所以我决定使用上面的代码。我的问题是,当我运行我的两个脚本时,一个将获得对程序的访问权限并按需要运行,但另一个将注意到错误,然后陷入无限循环,回显“等待访问程序”。

错过了什么? Statement 是在执行 CLI 命令还是只是返回到它的原始执行?还是我的问题出在其他地方?

最佳答案

您没有在某个地方的循环内更新您的 bay 变量。它设置一次并保持不变。每次都需要重新计算。

要么在循环内设置bay,要么在while的条件下设置。

while [ `prog -some flags` = "Another instance of this program is running, please exit it first" ]

编辑:

根据您的评论,您希望以后能够引用此输出。您可以回到原来的状态,但在阻塞循环内,将 bay=$(prog -some flags) 命令放入循环内。它会保留下来供您稍后使用。

bay=$(prog -some flags)
while [ $bay = "Another instance of this program is running, please exit it first" ]
do
echo "Awaiting Access to program"
bay=$(prog -some flags)
done
.....

关于linux - Bash Shell 执行 While Loop 无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7916822/

相关文章:

c++ - 使用管道通过 TTY、PTY 或 execv() 运行 shell(例如 sh)之间有什么区别?

linux - shell 脚本中的超时并报告那些超时的输入

c - 使用 C 在 Linux 中使用 USB 串行电缆从传感器串行读取

python - 获取 ffmpeg 的结果并将其传递给 python 脚本

不同用户的 PHP Cronjob

linux - 在 *nix shell 中更容易导航

bash - 仅按前两列对文件进行排序,并保持输入顺序,以防列具有相同的值

linux - 我的期望脚本关闭得太快

linux - 获取每个核心使用 BASH

shell -/usr/bin/env 有什么作用?