当使用 bash 版本 3.2 在 OSX 中运行以下脚本时
#!/bin/sh
set -e
function _trap_exit {
echo "this is a triggered trap..."
}
trap _trap_exit EXIT
/usr/sbin/ioreg -w0 -l | grep ExternalChargeCapable | grep -q No
echo "Final statement"
我触发了一个陷阱。但是,如果我将最终的 grep 语句更改为“是”,grep -q Yes
则不会触发陷阱。
知道为什么会发生这种情况以及如何阻止它吗?为什么 grep 的退出代码会导致陷阱被触发?
答案1
从 Bash 手册页...
-e Exit immediately if a simple 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 in
an if statement, part of a && or || list, or if the command's return value is
being inverted via !. A trap on ERR, if set, is executed before the shell
exits.
[/傻瓜]
抱歉浪费了您的时间...