Sign up ×
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.

A toy example:

I want to run the following commands sequentially

python generate.py 1
python simulate.py 1
python generate.py 2
python simulate.py 2
...
python generate.py 100
python simulate.py 100

Given the large number of commands, I'd like to use a for loop along the lines

for i in {1..100}; do \
    python generate.py $i; python simulate.py $i; \
done

It's very important for the jobs to run one after the next. Am I doing it correctly?

share|improve this question
1  
I wonder if the use of backslashes is necessary? –  mohammad .k yesterday
    
You don't need the backslashes, although using them is fine (except that it will look like you are joining them in one long line to the lexer). –  Arthur2e5 yesterday
9  
Better to join the commands with && to prevent the second running if the first fails. –  User112638726 yesterday

2 Answers 2

Yes, you are doing this correctly. The loop you've written will execute the commands in the exact order you listed them out long-hand, though it is important to note that an error in one command will not cause the loop to terminate, it will continue with the next command, effectively ignoring the error.

share|improve this answer
    
Thanks, John! If I may, I have another question. Previously, I had for i in {1..100}; do python generate.py $i python simulate.py $i done but it didn't work, difference being in the semi-colons. Sorry, I'm new to this so I couldn't create new lines correctly in the comment. Can you explain why? –  Godwin Yung yesterday
    
The problem was that you needed a "command seeparator" between the generate.py and the simulate.py scripts. Either a newline or a semicolon would have worked; you didn't need the semicolon at the end of that line (though having it didn't hurt anything). –  John yesterday
    
or, as User112638726 suggested above use && instead of ; so that simulate.py only runs if generate.py finishes without error. e.g. for i in {1..100}; do python generate.py $i && python simulate.py $i ; done –  cas yesterday
    
you can also use set -e before the for loop and set +e after it to cause the entire loop to exit if any error occurs in any of the generate.py or simulate.py instances. this may or may not be what you want. –  cas yesterday
    
Thanks for the suggestion, @cas and @User112638726. If I wanted each command to only run if every command finished without error, would it be for i in {1..100}; do python generate.py $i && python simulate.py $i && done? –  Godwin Yung 11 hours ago

Yes, you are. A simple way to check would be to echo the commands instead of running them:

$ for i in {1..5}; do 
     echo python generate.py "$i"
     echo python simulate.py "$i" 
 done
python generate.py 1
python simulate.py 1
python generate.py 2
python simulate.py 2
python generate.py 3
python simulate.py 3
python generate.py 4
python simulate.py 4
python generate.py 5
python simulate.py 5

As you can see in the above example, the commands are launched as you want them.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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