如何在if语句中捕获错误

时间:2012-10-27 20:03:50

标签: bash if-statement error-handling bash-trap

运行以下代码:

#!/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语句中捕获'命令未找到'错误?

2 个答案:

答案 0 :(得分:4)

您无法在if

中为测试捕获ERR

来自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 ..

答案 1 :(得分:0)

不幸的是,German Garcia是对的,所以我写了一个解决方法 任何建议或改进都非常受欢迎
这是我到目前为止所做的:

#!/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语句中进行调试。
我不能以这种方式捕获错误代码,但只要我能收到消息就没那么重要了
很高兴收到你的消息。感谢