Bash 脚本无故失败
Bash script failing of no reason
我有这个 bash 脚本(实际上是 https://github.com/ddollar/heroku-buildpack-multi/blob/master/bin/compile 的一部分,我自己添加了 echo):
echo "[DEBUG] chmod done"
framework=$($dir/bin/detect )
echo "[DEBUG] $framework done"
我在日志中看到:
[DEBUG] chmod done
Staging failed: Buildpack compilation step failed
而且我根本没有在日志中看到第二个回声。
不幸的是,我知道的不多bash。谁能向我解释在什么情况下第一个回声执行而第二个不执行?我一直认为无论第二行是否成功,这两个 echo 应该总是有效。
它在你的问题中不可见,但点击你的link,它在第三行说
set -e
这意味着每当发生错误时立即停止处理脚本。注释该行,脚本应该 运行 通过并打印第二个 echo
语句。
请注意,我没有检查脚本实际做了什么,我无法告诉您注释掉 set -e
是否真的是个好建议。
来自man set
:
−e: When this option is on, when any command fails (for any of the reasons listed
in Section 2.8.1, Consequences of Shell Errors or by returning an exit status
greater than zero), the shell immediately shall exit with the following excep‐
tions:
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.
2. 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.
3. 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
the false command causes the subshell to exit without executing echo one; how‐
ever, echo two is executed because the exit status of the pipeline (false; echo
one) | cat is zero.
我有这个 bash 脚本(实际上是 https://github.com/ddollar/heroku-buildpack-multi/blob/master/bin/compile 的一部分,我自己添加了 echo):
echo "[DEBUG] chmod done"
framework=$($dir/bin/detect )
echo "[DEBUG] $framework done"
我在日志中看到:
[DEBUG] chmod done
Staging failed: Buildpack compilation step failed
而且我根本没有在日志中看到第二个回声。 不幸的是,我知道的不多bash。谁能向我解释在什么情况下第一个回声执行而第二个不执行?我一直认为无论第二行是否成功,这两个 echo 应该总是有效。
它在你的问题中不可见,但点击你的link,它在第三行说
set -e
这意味着每当发生错误时立即停止处理脚本。注释该行,脚本应该 运行 通过并打印第二个 echo
语句。
请注意,我没有检查脚本实际做了什么,我无法告诉您注释掉 set -e
是否真的是个好建议。
来自man set
:
−e: When this option is on, when any command fails (for any of the reasons listed in Section 2.8.1, Consequences of Shell Errors or by returning an exit status greater than zero), the shell immediately shall exit with the following excep‐ tions:
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. 2. 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. 3. 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 the false command causes the subshell to exit without executing echo one; how‐ ever, echo two is executed because the exit status of the pipeline (false; echo one) | cat is zero.