Bash Shell Do While Loop 无限循环?

发布于 2024-12-12 08:25:01 字数 452 浏览 0 评论 0原文

基本上这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪痕残 2024-12-19 08:25:01

您没有在循环内的某处更新您的 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
.....

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.

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

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.

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
.....
阳光的暖冬 2024-12-19 08:25:01

更多DRY,我不会先敲敲代码,而是等待用户先做某事:

while true
do
  bay=$(prog -some flags)
  case "$bay" in
    "Another instance of this program is running, please exit it first")
      read -p "Awaiting Access to program. Close it and hit enter: " x ;;
    *) break ;;
  esac
done
echo "Results: $bay"

More DRY and instead of hammering prog, I'd wait for the user to do something first:

while true
do
  bay=$(prog -some flags)
  case "$bay" in
    "Another instance of this program is running, please exit it first")
      read -p "Awaiting Access to program. Close it and hit enter: " x ;;
    *) break ;;
  esac
done
echo "Results: $bay"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文