繁体   English   中英

Shell脚本 - 如果子节点无法执行,如何终止父节点

[英]Shell script — How to terminate parent if child fails to execute

我有一个shell脚本(父),它调用其他一些shell脚本。 假设子shell脚本无法执行,那么也应该停止父shell脚本而不执行下一个子shell脚本。 如何自动完成此过程?

例如:

main.sh
//inside the main.sh following code is there
child1.sh //executed successfully
child2.sh //error occurred
child3.sh //Skip this process
//end of main.sh

最简单的机制是:

set -e

这意味着只要子进程以失败状态退出,shell就会退出,除非状态作为条件的一部分进行测试。

例1

set -e
false                        # Exits
echo Not executed            # Not executed

例2

set -e
if false                     # Does not exit
then echo False is true
else echo False is false     # This is executed
fi
child1.sh && child2.sh && child3.sh

在上面的child2.sh只有在child1.sh成功完成时执行,并且只有在child2.sh成功完成时才执行child3.sh。

或者:

child1.sh || exit 1
child2.sh || exit 1
child3.sh || exit 1

在上面,父脚本在任何子节点失败后退出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM