Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

The following bash permits of running the commands 1,2,3 in all the folders that has names included in the file "list.txt"

#!/bin/bash
cat list.txt | while read i 
do
command1 &
command2 &
command3 &
done

The commands 1,2,3 in the code are running together in all the folders that has names included in the file "list.txt" . How can the commands run one by one in all the folders (i.e. when the first command finish the second command start and so on...). I tried to use "wait" between the commands but it didn't work! The solution suggested here Executing commands consequtively on multiple folders didn't work well! I am looking for another solution

share|improve this question

marked as duplicate by steeldriver, Jeff Schaller, mdpc, taliezin, Archemar 11 hours ago

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Don't put them in the background? – Jeff Schaller 22 hours ago
    
"... didn't work well' ? can you edit you first post and specify ? – Archemar 11 hours ago
up vote 3 down vote accepted
while read -r i #read directory as a raw line
do
    cd "$i"     #cd into the directory
    command1    #run the commands consecutively
    command2
    command3
    cd -        #cd back
done < list.txt

If you want to run process all the directories simultaneously (with command1 throught 3 staying consecutive), then replace:

    command1   
    command2
    command3

with

    (
    command1   
    command2
    command3
    )&
share|improve this answer
    
Thank you PSkocik. I want command 1 to run in all the folders in the loop. When command 1 is done. The command 2 will start also in all the folders in the loop and so on. – goro 22 hours ago
1  
Just an FYI; cd - will return to the previous directory in many shells (eg bash, ksh) so the pwd=$PWD and cd $pwd is simpler. – Stephen Harris 22 hours ago
    
@goro Oh, I see. Then make it three loops. – PSkocik 22 hours ago
    
Great!!! that's it . I was looking for it for a while. I highly appreciate your help :) – goro 22 hours ago
1  
The usual way to change working directory for the execution of one or a few commands in a script is to do it in a subshell: (cd dir && { command; command; command; }& ). That way you don't have to remember to change back again afterwards. – Kusalananda 21 hours ago

If you just want to run them in sequence, but still in the background, and you don't care about the return value of each command, you can do this:

#!/bin/bash
cat list.txt | while read i 
do
    { command1; command2; command3; } &
done

If you only want command2 to run if command1 was successful, you can use &&:

#!/bin/bash
cat list.txt | while read i 
do
    command1 && command2 && command3 &
done

If you want to run everything synchronously and in the foreground, just remove the & altogether:

#!/bin/bash
cat list.txt | while read i 
do
    command1
    command2
    command3
done
share|improve this answer
    
I want command 1 to run in all the folders in the loop. When command 1 is done. The command 2 will start also in all the folders in the loop and so on. – goro 22 hours ago
    
{ ... } & vs ( ... ) & ? – cat 22 hours ago
    
{ ... } normally doesn't require a subshell, whereas ( ... ) does. But when you put an & after it, it might create a subshell anyway--it at least creates a background process. – Will 21 hours ago

Remove the & to have the next command executed after the previous is finished.

share|improve this answer

If you want the remaining commands to be executed only if the previous ones succeeded then the && operator is the way to go.

command 1 &&
command 2 &&
command 3

then command 2 will only be run if command 1 returns 0 (that is succeeds) and command 3 will only be run if command 2 returns 0. And that implies that command 2 waits until command 1 has finished running.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.