2

I realized that the following loop in a bash script will send ./a.out to background, and the run will return to the system before even a single ./a.out running is done.

#!/bin/bash for i in 1,2,3 do echo $i ./a.out done 

The question is how to let the next bash loop wait until "./a.out" is done?

BTW, I thought this should be a common problem but didn't find similar questions, or I may need more searching skills...

1
  • 2
    Re: "the following loop in a bash script will send './a.out' to background": This is not true. What makes you think it is? (Also, you probably meant to write for i in 1 2 3 rather than for i in 1,2,3.) Commented Jan 26, 2013 at 0:10

2 Answers 2

2

What you're describing is the default behavior. You might be confused by the fact that your loop only runs once, with i="1,2,3", and therefore appears to print all at once and then exit immediately.

Try for i in 1 2 3 instead, and see if you get the output you expect.

Sign up to request clarification or add additional context in comments.

4 Comments

It worked! Could you explain more about the difference between "for i in 1,2,3" and "for i in 1 2 3"?
The syntax is for i in tokens... Tokens are separated by whitespace, not commas or anything else.
I see, "1,2,3" is a single object in "for i in 1,2,3".
@HailiangZhang: If you're used to "normal" programming languages, one of the things that may throw you about shell scripting is that spaces aren't just something you put in to make the code easier to read; they're the primary delimiter in shell syntax. I see a lot of questions here that come from people getting scripts wrong, either because they left out required spaces, or added them someplace where they're forbidden. In the shell, spaces matter.
1

You may find the "C style" for loop easier, ie: for ((i = 1 ; i < 4 ; i++))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.