Bash Shell Do While Loop 无限循环?
基本上这是我的代码:
bay=$(prog -some flags)
while [ $bay = "Another instance of this program is running, please exit it first" ]
do
echo "Awaiting Access to program"
do
.....
我有一个程序,由于它与我的硬件交互的方式,它一次只允许一个实例运行,当另一个实例运行时,它会弹出以下消息“该程序的另一个实例正在运行” ,请先退出”。
我需要能够运行多个使用同一程序的脚本,因此我决定使用上面的代码。我的问题是,当我运行两个脚本时,一个脚本将获得对程序的访问权限并根据需要运行,但另一个脚本会注意到错误,然后陷入无限循环,回显“等待访问程序”。
是不是错过了什么?该语句是执行 CLI 命令还是只是返回到其原始执行?还是我的问题出在其他地方?
Basically this is my code:
bay=$(prog -some flags)
while [ $bay = "Another instance of this program is running, please exit it first" ]
do
echo "Awaiting Access to program"
do
.....
I have program which will only allow one instance to be run at one time due to the way it interacts with my hardware, when another instance is running it kicks out the following message "Another instance of this program is running, please exit it first".
I need to beable to run multiple scripts which will utilise this same program so I've decided to use the above code. My problem is that when I run my two scripts one will gain access to the program and run as desired but the other will notice the error and then get stuck in an infinate loop echoing "Awaiting Access to program".
Have missed some thing? Is the Statement executing the CLI command or just reffering back to its origanal execution? Or is my problem else where?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有在循环内的某处更新您的
bay
变量。它设置一次并保持不变。每次都需要重新计算。要么在循环中设置
bay
,要么在 while 条件中设置。编辑:
根据您的评论,您希望稍后能够引用此输出。您可以返回到原来的状态,但在阻塞循环内,将
bay=$(prog -some flags)
命令放入循环内。它会保留下来供您以后使用。You are not updating your
bay
variable inside of the loop somewhere. It gets set once and stays the same. You need to recalculate it every time.Either set
bay
within the loop, or in the condition of the while.Edit:
From your comment, you want to be able to reference this output later. You can go back to what you had, but inside of your blocking loop, put your
bay=$(prog -some flags)
command inside of the loop. It will stick around for you to use later.更多DRY,我不会先敲敲代码,而是等待用户先做某事:
More DRY and instead of hammering prog, I'd wait for the user to do something first: