基本上这是我的代码:
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/