不幸的是,德国加西亚是对的,所以我写了一个解决方法。
任何建议或改进都非常受欢迎,谢谢。
这是我到目前为止所做的:
#!/bin/bash
set -o pipefail
set -o errtrace
set -o nounset
set -o errexit
declare stderr_log="/dev/shm/stderr.log"
exec 2>"${stderr_log}"
function err_handler ()
{
local error_code=${1:-$?}
echo "TRAP! ${error_code}"
echo "exit status: $error_code"
stderr=$( tail -n 1 "$stderr_log" )
echo "error message: $stderr"
echo "" > "${stderr_log}"
echo "Normally I would exit now but I carry on the demo instead"
# uncomemment the following two lines to exit now.
# rm "${stderr_log}"
# exit "${error_code}"
}
trap err_handler ERR
function check ()
{
local params=( "$@" )
local result=0
local statement=''
for param in "${params[@]}"
do
local regex='\s+'
if [[ ${param} =~ ${regex} ]]
then
param=$( echo "${param}" | sed 's/"/\\"/g' )
param="\"$param\""
fi
statement="${statement} $param"
done
eval "if $statement; then result=1; fi"
stderr=$( tail -n 1 "$stderr_log" )
($statement); local error_code="$?"
test -n "$stderr" && err_handler "${error_code}"
test $result = 1 && [[ $( echo "1" ) ]] || [[ $( echo "" ) ]]
}
echo -e "\n1) wrong command in if statement"
if check xsxsxs -d "/etc"
then
echo "if returns true"
else
echo "if returns false"
fi
echo -e "\n2) right command in if statement"
if check test -d "/etc"
then
echo "if returns true"
else
echo "if returns false"
fi
echo -e "\n3) wrong command directly"
xsxsxsxs
exit
运行上述将产生:
1) wrong command in if statement
TRAP!
error code found: 0
error message: trap.sh: line 52: xsxsxs: command not found
I would exit now but I carry on instead
if returns false
2) right command in if statement
if returns true
3) wrong command directly
TRAP!
error code found: 127
error message: trap.sh: line 77: xsxsxsxs: command not found
I would exit now but I carry on instead
所以这个想法基本上是创建一个名为'check'的方法,然后将其添加到命令之前,以便在 if 语句中进行调试。
我无法以这种方式捕获错误代码,但只要我能收到消息,这并不重要。
很高兴收到您的来信。谢谢