1

How can I use the loop "until .... do" with a for loop?.

For example, in the following for loop, the "for loop" and the commands "command # 5, #6, #7" are runnig at once.

 For i in $(cat file.txt), do  # file.txt is a file contain folders names within a path
 (
 command 1
 command 2
 command 3
 command 4
 ...
 )&
 done
 command5
 command6
 command7
 .....

I want to use "until the "for loop" is done", then "run the commands #5, #6, #7"

What is wrong with usage of until .. do in this code?

until 
For i in $(cat file.txt), do  # file.txt is a file contain folders names within a path
 (
 command 1
 command 2
 command 3
 command 4
 ...
 )&
 done
 do
 command5
 command6
 command7
 .....
0

You haven't given it an actual condition to meet. See here for the syntax.

For this particular case you could do something like this:

done_cond=0

until $done_cond==1; do
    For every variable name saved in a text file, do 
    echo "variable name"
    done

    $done_cond=1
done

echo 'done'

In this particular case, done_cond will cause it to not break out until the for loop has finished but there are a number of ways to achieve similar effects for different use cases.

Of course, if it's just a for loop you wouldn't need 'until' in the first place.

The first block of code you posted, with a semi-colon (;) instead of a comma (,) before the do will work.

| improve this answer | |
  • Thank you .I revised the question to include actual code – user88036 Aug 18 '16 at 16:09
  • What is the optimal solution in my example? – user88036 Aug 18 '16 at 16:11
  • No problem. For loops will run until completion anyway so I'm not sure why you're using until? – I_GNU_it_all_along Aug 18 '16 at 16:11
  • The command echo "done" and for loop are running at once – user88036 Aug 18 '16 at 16:12
  • In the first block of code in your post, change the comma to a semi-colon (;) and that should work. – I_GNU_it_all_along Aug 18 '16 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy