为什么不设置-e导致`false ||失败虚假的是吗? [英] Why doesn't set -e cause a failure with `false || false && true`?

查看:106
本文介绍了为什么不设置-e导致`false ||失败虚假的是吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找不到合适的标题,我不理解破折号/重击的行为.也就是说,如果命令失败,我将使用set -e来纾困,并使用命令组来处理肯定的结果.

Can't figure out a fitting title, I don't understand the behavior in dash/bash. Namely I am using set -e to bail out if a command fails, and command groups to handle the positive result.

即.总体方案是:

[ ! wantcommand ] || command

比表示仅在需要时执行命令,失败将自动终止脚本.

Than means the command only gets executed if needed, and a failure will automatically kill the script.

可能需要进行一些后处理,在这种情况下,我将使用以下方法:

There might be some postprocessing necessary, in that case I use this:

[ ! wantcommand ] || { command && postprocess; }

这导致了一些奇怪的错误搜寻,因为这不会杀死外壳,我无法理解原因.我现在必须仔细阅读一些shell代码,但想了解原因.

This has led to some curious bughunting, as this wont kill the shell and I cant get behind the reason. I have to go through some chunks of shell code now, but would like to understand the reason.

进行测试:

bash -c 'set -e; { false || false && echo "post" ; }; echo "ec $?"'

或:

bash -c 'set -e; { set -e; false || false && echo "post" ; }; echo "ec $?"'

注意:我不是在要求修复,而是主要为什么返回码为1,但shell无法退出

Note: I am not asking for a fix, but primary why the returncode is 1, but the shell wont quit

推荐答案

set -e仅在未经检查失败时保释.

分支到故障(使用ifuntilwhile&&||)时,将检查该故障.

When you branch on a failure (using if, until, while, && or ||), that failure is checked.

如果规范不是以这种方式编写的,则短路布尔运算将不能有效地用于流控制,因为错误的分支总是会导致退出.

If the specification were not written in this manner, short-circuiting boolean operations could not effectively be used for flow control because the false branches would always cause an exit.

要引用规范,并重点强调:

启用此选项后,任何命令都会失败(出于 Shell错误的后果或通过返回大于零的退出状态),Shell应当立即退出,就像通过执行不带参数的退出特殊内置实用程序一样,但以下情况除外:

When this option is on, when any command fails (for any of the reasons listed in Consequences of Shell Errors or by returning an exit status greater than zero), the shell immediately shall exit, as if by executing the exit special built-in utility with no arguments, with the following exceptions:

  1. 多命令管道中任何单个命令的失败都不会导致外壳退出.仅考虑管道本身的故障.

  1. The failure of any individual command in a multi-command pipeline shall not cause the shell to exit. Only the failure of the pipeline itself shall be considered.

在执行whileuntilifelif保留字,以!保留字开头的管道之后的复合列表时,应忽略-e设置,或 AND-OR列表中除最后一个命令之外的任何命令.

The -e setting shall be ignored when executing the compound list following the while, until, if, or elif reserved word, a pipeline beginning with the ! reserved word, or any command of an AND-OR list other than the last.

如果在忽略-e时,除subshel​​l命令以外的复合命令的退出状态是失败的结果,则-e不适用于该命令.

If the exit status of a compound command other than a subshell command was the result of a failure while -e was being ignored, then -e shall not apply to this command.

此要求分别适用于外壳环境和每个子外壳环境.例如,在:

This requirement applies to the shell environment and each subshell environment separately. For example, in:

set -e; (false; echo one) | cat; echo two

false命令使子Shell退出而不执行echo one;但是,执行echo two是因为管道(false; echo one) | cat的退出状态为零.

the false command causes the subshell to exit without executing echo one; however, echo two is executed because the exit status of the pipeline (false; echo one) | cat is zero.

请注意,此规范已随时间而变化;实现POSIX规范的先前版本的shell可能与此处引用的版本不完全一致.

Note that this specification has changed over time; shells implementing a prior revision of the POSIX specification may not precisely comply with the version quoted here.

要在此处提出一些意见-我强烈建议阅读 BashFAQ#105 和确保在决定使用set -e之前而不是手动实施显式错误处理之前,完全了解其中描述的所有行为. FVUE Wiki 进一步描述了bash本机模式下的set -e与POSIX在行为上的区别.模式,同样应该理解.

To inject some opinion here -- I'd strongly suggest reading BashFAQ #105 and ensuring that you fully understand all behaviors described therein before making the decision to use set -e rather than implementing explicit error-handling by hand. The FVUE wiki further describes the distinctions in behavior between set -e in bash-native mode and POSIX mode, which should likewise be understood.

这篇关于为什么不设置-e导致`false ||失败虚假的是吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆