0

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.

bigschatz
  • 25
  • 6
  • 2
    `bash ./my_text_file_with_python_commands | results.txt` ? – Jetchisel May 17 '20 at 21:51
  • Maybe you'd be interested in using a loop to [execute a command on each file in a directory](https://stackoverflow.com/questions/10523415/execute-command-on-all-files-in-a-directory), or [running a command n times](https://stackoverflow.com/questions/3737740/is-there-a-better-way-to-run-a-command-n-times-in-bash) where the number can be used in the command – that other guy May 17 '20 at 21:52

2 Answers2

1

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.

M Laing
  • 52
  • 5
  • I ran your suggestion at the command line and it is giving me syntax error here: `SyntaxError: invalid syntax File "dna.py", line 32 dna_sequences = open(f"{sys.argv[1]}", "r").readline().strip()` My code seems correct and runs in the IDE with no errors – bigschatz May 17 '20 at 22:10
  • Did you save the "your_file_name_with_python_code_in_it" file in the same folder as "dna.py"? If not, it should be as I see you use relative path names to databases and sequences folders. – M Laing May 17 '20 at 22:21
  • yes it is in the same folder. For some reason it does not like this line: `f"{argv[1]}", "r").readline().strip()` However the program works in the ide when running each of those commands individually at the command line – bigschatz May 17 '20 at 22:24
  • This worked when I changed the command from "python" to "python3" in the text file – bigschatz May 17 '20 at 23:16
1

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.

Jetchisel
  • 7,493
  • 2
  • 19
  • 18
Mahmoud Odeh
  • 942
  • 1
  • 7
  • 19