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

为什么bash errexit在函数调用中的表现不如预期?

发布时间:2020-12-16 01:08:50 所属栏目:安全 来源:网络整理
导读:在bash手册页中,它指出: Exit immediately if a pipeline (which may consist of a single simple command), a subshell command enclosed in parentheses,or one of the commands executed as part of a command list enclosed by braces… 所以我假设一个
在bash手册页中,它指出:

Exit immediately if a pipeline (which may consist of a single simple command),
a subshell command enclosed in parentheses,or one of the commands executed as
part of a command list enclosed by braces…

所以我假设一个函数应该被视为括号括起来的命令列表.但是,如果对函数调用应用条件,则errexit不再保留在函数体内,并且在返回之前执行整个命令列表.即使您在为该子shell启用了errexit的函数内显式创建子shell,也会执行命令列表中的所有命令.这是一个演示该问题的简单示例:

function a() { b ; c ; d ; e ; }
function ap() { { b ; c ; d ; e ; } ; }
function as() { ( set -e ; b ; c ; d ; e ) ; }
function b() { false ; }
function c() { false ; }
function d() { false ; }
function e() { false ; }
( set -Eex ; a )
+ a
+ b
+ false
( set -Eex ; ap )
+ ap
+ b
+ false
( set -Eex ; as )
+ as
+ set -e
+ b
+ false

现在,如果我对每个人都应用条件……

( set -Eex ; a || false )
+ a
+ b
+ false
+ c
+ false
+ d
+ false
+ e
+ false
+ false
( set -Eex ; ap || false )
+ ap
+ b
+ false
+ c
+ false
+ d
+ false
+ e
+ false
+ false
( set -Eex ; as )
+ as
+ set -e
+ b
+ false
+ c
+ false
+ d
+ false
+ e
+ false
+ false
你开始引用 manual然后你切掉了解释这种行为的位,这是在下一句话中:

-e Exit immediately if a pipeline,which may consist of a single simple command,a subshell command enclosed in parentheses,or one of the commands executed as part of a command list enclosed by braces returns a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword,part of the test in an if statement,part of any command executed in a && or || list except the command following the final && or ||,any command in a pipeline but the last,or if the command’s return status is being inverted with !.

(编辑:李大同)

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

    推荐文章
      热点阅读