bigschatz
bigschatz

Reputation: 35

Running repetitive commands in terminal

I am somewhat new to running shell commands. Currently I am executing these one at a time in terminal like this:

python dna.py databases/small.csv sequences/1.txt
// result

python dna.py databases/small.csv sequences/2.txt
// result

python dna.py databases/small.csv sequences/3.txt
// result

etc...

Is there a way to put all these commands in a text file and run a single command that will execute them all at once? Lastly, I'd like to pipe this to a results.txt file.

Upvotes: 0

Views: 286

Answers (2)

Mahmoud Odeh
Mahmoud Odeh

Reputation: 950

you can iterate over sequences generated on the shell seq

repeat=10
for n in $(seq 1 $repeat);  do python dna.py databases/small.csv sequences/${n}.txt; done

as per Jetchisel suggestion, you can brace expansion which is a bash4 feature, that was added in 2009.

python dna.py databases/small.csv sequences/{1..3}.txt >> results.txt

With the bash c-style for-loop

start=1 end=3
for ((n=start;n<=end;n++)); do python dna.py database/small.csv sequences/"$n".txt

this would block your terminal until your process finished and would be considered a foreground process with no user interaction. The point here being whether the process blocks the execution of other processes until it terminates.

you can make a foreground process into a background one by adding & at the end of your command line.

Upvotes: 1

M Laing
M Laing

Reputation: 52

Use a text editor of your choice and paste the three commands below each other. Save that file and open a terminal window. Change to the folder containing the file you just created and run "chmod +x your_file_name_with_python_code_in_it".

Then run that file in the terminal "./your_file_name_with_python_code_in_it >> results.txt"

The >> will create and append to the log file what your python code feeds back to the console.

Upvotes: 1

Related Questions