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

前端之家收集整理的这篇文章主要介绍了为什么bash errexit在函数调用中的表现不如预期?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在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 !.

原文链接:https://www.f2er.com/bash/384590.html

猜你在找的Bash相关文章