gpt4 book ai didi

bash - 使用 && 流水线时,采购 bash 脚本不会出错

转载 作者:行者123 更新时间:2023-11-29 09:22:41 26 4
gpt4 key购买 nike

我发现在使用 && 进行流水线处理时,获取以下 bash 脚本不会导致命令序列停止。

sourceme.sh:

#!/usr/bin/env bash
set -o errexit
set -o | grep errexit

echo "About to error from sourceme.sh..."
$(false)

echo "sourceme.sh did not exit on error (exit code: $?)"

这是演示问题的调用。

(source sourceme.sh && echo "sourceme.sh did not error (exit code: $?)")

我从中得到的输出如下。

errexit         on
About to error from sourceme.sh...
sourceme.sh did not exit on error (exit code: 1)
sourceme.sh did not error (exit code: 0)

这与我的预期相反,我希望在 sourceme.sh 中到达 $(false) 时看到整个命令终止,生成跟随输出。

errexit         on
About to error from sourceme.sh...

继续奇怪,如果我使用 ; 代替 &&,那么我希望命令继续,但事实并非如此。

$ (source sourceme.sh && echo "sourceme.sh did not error (exit code: $?)")
errexit on
About to error from sourceme.sh...

因此,当获取文件时,&& 的行为与我期望的一样 ; 的行为,而 ; 的行为与我期望的 && 行为。

我对这应该如何工作的理解哪里出了问题?

最佳答案

粗略地说,这个想法是set -o errexit (或者,等效地,set -e)导致 shell 在出现未捕获 错误时退出。事实&&关注source sourceme.sh意味着错误被捕获。

所以,如果 sourceecho命令与 ; 相关联,外壳退出:

$ (source sourceme.sh ; echo "sourceme.sh did not error (exit code: $?)")
errexit on
About to error from sourceme.sh...

但是,如果 sourceecho命令与 && 相关联, 然后 shell 假定您已经预料到 source 的可能性命令失败,shell 不退出:

$ (source sourceme.sh && echo "sourceme.sh did not error (exit code: $?)")
errexit on
About to error from sourceme.sh...
sourceme.sh did not exit on error (exit code: 1)
sourceme.sh did not error (exit code: 0)

set -e 的大部分行为可能被认为是奇怪的或出乎意料的。有关更多示例,请参阅 Why doesn't set -e (or set -o errexit, or trap ERR) do what I expected? .

文档

set -e 的行为(errexit) 在 man bash 中有更精确的定义:

-e
Exit immediately if a pipeline (which may consist of a single simple command), a list, or a compound command (see SHELL GRAMMAR above), exits with 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 following the if or elif reserved words, 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 value is being inverted with !. If a compound command other than a subshell returns a non-zero status because a command failed while -e was being ignored, the shell does not exit. A trap on ERR, if set, is executed before the shell exits. This option applies to the shell environment and each subshell environment separately (see COMMAND EXECUTION ENVIRONMENT above), and may cause subshells to exit before executing all the commands in the subshell.

If a compound command or shell function executes in a context where -e is being ignored, none of the commands executed within the compound command or function body will be affected by the -e setting, even if -e is set and a command returns a failure status. If a
compound command or shell function sets -e while executing in a context where -e is ignored, that setting will not have any effect until the compound command or the command containing the function call completes.

同义词

man bash 中所述以及 help set ,下面两条命令是等价的:

set -e
set -o errexit

关于bash - 使用 && 流水线时,采购 bash 脚本不会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49292357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com