有两个问题:
-
你跑吧
handleExit
跑步前
wrapper
,所以它还没有失败。
-
handleExit公司
检查的退出代码
echo
如果没有描述你想做什么,我猜你想要:
#!/bin/bash
set -euo pipefail
logError() {
# A better way to write to stderr
echo "$*" >&2
}
function writeToErr () {
echo "standard out"
logError "standard err"
exit 1
}
function wrapper () {
writeToErr >>output.log 2>&1
}
function handleExit () {
# Get exit code of the previous command, instead of echo
exitCode=$?
echo "handle exit"
if [ $exitCode -eq "0" ]
then
echo "No problem"
else
echo "$0 exited unexpectedly with status:$exitCode"
exit 1
fi
}
# Call handler on exit, so it has something to handle
trap handleExit EXIT
wrapper