如何在IF语句中捕获错误



运行以下代码:

#!/bin/bash
set -o pipefail
set -o errtrace
set -o nounset
set -o errexit
function err_handler ()
{
    local error_code="$?"
    echo "TRAP!"
    echo "error code: $error_code"
    exit
}
trap err_handler ERR
echo "wrong command in if statement"
if xsxsxsxs 
    then
        echo "if result is true"
    else
        echo "if result is false"
fi
echo -e "nwrong command directly"
xsxsxsxs 
exit


产生以下输出:

wrong command in if statement
trap.sh: line 21: xsxsxsxs: command not found
if result is false
wrong command directly
trap.sh: line 29: xsxsxsxs: command not found
TRAP!
error code: 127


我如何在if语句中也未找到'命令'错误?

您不能在if

中捕获测试

来自Bash Man:

The ERR trap is not executed if the failed  command
is  part  of  the  command list immediately following a while or
until keyword, part of the test in an if statement,  part  of  a
command  executed in a && or || list, or if the command's return
value is being inverted via !

但是您可以更改此

if xsxsxsxs 
then ..

到这个

xsxsxsxs 
if [[ $? -eq 0 ]]
then ..

不幸的是,德国加西亚是对的,所以我写了一个解决方法。
任何建议或改进都非常欢迎。

这是我到目前为止所做的:

#!/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语句中调试的命令。
我无法以这种方式捕获错误代码,但是只要我能收到消息就没关系。

很高兴收到您的来信。谢谢

相关内容

  • 没有找到相关文章

最新更新