当其中的任何命令失败时,如何使Shell脚本退出?

时间:2019-04-02 13:53:37

标签: bash shell unix

我正在尝试在bash shell上创建一个脚本,其中包含4个python脚本,为了对它们进行顺序排序,我对其进行了排序,如果其中任何一个未执行或未返回任何错误,该如何确定条件,然后停止壳

#!/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

1 个答案:

答案 0 :(得分:1)

在命令之间插入&&会告诉bash从头到尾执行命令,如果在此过程中发生任何故障,它将停止执行。

例如:

echo $(non_existent_variable) && cd ~

cd ~是有效命令。但是,由于未定义non_existent_variable,因此第一个命令失败,并立即引发错误。

根据您的情况,您可以在每一行的末尾添加&& \

相关问题