加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 00:30:33 所属栏目:Linux 来源:网络整理
导读:我有一个 shell脚本(父),它调用其他一些shell脚本.假设子shell脚本无法执行,那么也应该停止父shell脚本而不执行下一个子shell脚本.如何自动完成此过程? 例如: main.sh//inside the main.sh following code is therechild1.sh //executed successfullychild
我有一个 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读