Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run Python script in Bash loop

Tags:

python

bash

loops

I have a python script, which can only be executed a finite number of times before it throws an error (on the terminal). I need to execute it those many times.

Is it possible to do this using a loop in a Bash script? I know a Bash script can read the output of a command from the terminal, but I have no idea how to go about this.

I tried searching for this problem on this site, but I found nothing. Sorry if this has been asked before.

EDIT

I can modify the Python script to return a value depending whether the execution is successful or not. What I want to know is, how to read this returned value in a loop in a Bash script. If the execution was successful, it must execute the Python script again. If not, it must exit.

If there is any other way, that is welcome, too.

like image 240
tfpf Avatar asked Nov 30 '25 15:11

tfpf


1 Answers

Right on the command line

user@darkstar:~$ for i in `seq 1 100`; do ./run_the_python; done

Replace 100 with your max value, ./run_the_python with whatever is appropriate to launch your python script.

Update 20190625 /bin/sh: syntax error: unexpected ";"

like image 164
ivanivan Avatar answered Dec 02 '25 05:12

ivanivan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!