0

I have a bash script that executes a number python scripts. Is there a way to prevent the execution of script_2.py if 1_script.py throws back an exception?

#!/usr/bin/bash

python Desktop/code/1_script.py
python Desktop/code/2_script.py
python Desktop/code/3_script.py

Thanks very much!

ruohola
  • 21,987
  • 6
  • 62
  • 97
Jkennedy559
  • 134
  • 1
  • 9

2 Answers2

2

You can simply use && to only execute the next command if the previous one completed succesfully (0 exit code).

#!/usr/bin/bash

python Desktop/code/1_script.py && python Desktop/code/2_script.py && python Desktop/code/3_script.py
ruohola
  • 21,987
  • 6
  • 62
  • 97
  • Thanks very much for the response, I have done a test for when one of the scripts throws an exception on execution with your solution. In this scenario my gitbash terminal just froze and the prompt didn't return. Should the bash script return an error message or what ought to happen if one of the python scripts doesn't run without exception? – Jkennedy559 Sep 11 '19 at 10:37
-2

DO step by step At first execute 1_script.py then, execute another python files.

OR

python Desktop/code/1_script.py && python Desktop/code/2_script.py && python Desktop/code/3_script.py

OR

write set -e at the top of your script 

"Thanks"

Yasen
  • 4,241
  • 1
  • 16
  • 25
Sumit Raj
  • 1
  • 1