Bash Shell Do While Loop Endless Loop?

This is basically 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 a program that will allow only one instance to start at a time due to the way it interacts with my equipment, when another instance starts it, it displays the following message "Another instance of this program is running, please exit first."

I need to execute several scripts that will use the same program, so I decided to use the code above. My problem is that when I run my two scripts, you get access to the program and execute as you wish, and the other will notice an error and then get stuck in the infinate loop, echoing "Waiting for access to the program."

Have you missed something? Is the Statement an execution of the CLI command, or is it just returning to its original execution? Or where else is my problem?

+4
source share
2 answers

You are not updating the bay variable inside the loop. It is installed once and remains unchanged. You need to recount it every time.

Either set bay inside the loop, or in the while state.

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

Edit:

From your comment, you want to get a link to this conclusion later. You can go back to what you had, but inside your lock loop put your bay=$(prog -some flags) command bay=$(prog -some flags) inside the loop. It will be used for you 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 ..... 
+7
source

More DRY and instead of hammering the prog, I would wait for the user to do something in the first place:

 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" 
+4
source

All Articles