3

运行以下代码:

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

4

2 回答 2

5

您不能在 if 中为测试捕获 ERR

来自 bash 人:

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 ..
于 2012-10-27T21:32:47.510 回答
1

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

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

#!/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 语句中进行调试。
我无法以这种方式捕获错误代码,但只要我能收到消息,这并不重要。

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

于 2012-10-29T18:04:49.280 回答