0

I'm trying to create a script on bash shell, containing 4 python scripts, I have sorted them in order to execute them sequentially, how do I make a condition if anyone of them not executed or returned any error then stop the shell

#!/bin/bash -x

cd /home/hadoop/Traffic_Stat_Process

python3 Table_Dropper.py
/usr/bin/spark-submit --jars /home/hadoop/vertica/lib/vertica-jdbc-9.1.1-0.jar Spark_JDBC_Connector.py
hadoop fs -rm -r /hadoopData/hive/warehouse/temp
python3 2th_phase.py
python3 3th_phase.py
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Mahmoud Odeh
  • 942
  • 1
  • 7
  • 19
  • `if ! python3 arg; then echo "python3 arg failed" >&2; exit 1; fi`. – KamilCuk Apr 02 '19 at 13:56
  • but the 2th script executed by spark-submit not python3 – Mahmoud Odeh Apr 02 '19 at 14:03
  • `if ! /usr/bin/spark-submit --jars /home/hadoop/vertica/lib/vertica-jdbc-9.1.1-0.jar Spark_JDBC_Connector.py ; then echo "the spark-submit failed" >&2; exit ; fi` – KamilCuk Apr 02 '19 at 14:06
  • Put `|| exit` after any command you want to terminate the script on exit; there's also `set -e`, but there are [compelling reasons to avoid it](http://mywiki.wooledge.org/BashFAQ/105#Exercises). Nothing about this is specific to Python, so the title can probably be focused a bit. – Charles Duffy Apr 02 '19 at 14:27

1 Answers1

1

Putting && between commands will tell bash to execute commands from start to end, and if any fail along the way, it will stop executing.

For example:

echo $(non_existent_variable) && cd ~

cd ~ is a valid command. However, since non_existent_variable is not defined, the first command fails and an error is thrown immediately.

In your case, you can add && \ at the end of each of your lines.

  • In [How to Answer](https://stackoverflow.com/help/how-to-answer), note the section "Answer Well-Asked Questions", and therein the bullet point regarding questions which "have already been asked and answered many times before". – Charles Duffy Apr 02 '19 at 14:29