2

我想在出现错误时完全终止/退出 bash shell 脚本,但是使用一个error可以让我在终止之前显示调试输出的函数。现在我有一个问题,如果exit 1函数输出是通过反引号或$().

这是我的示例脚本:

    #!/bin/bash

    function error ()
    {
            echo "An error has occured: $1"
            exit 1
    }
    function do_sth ()
    {
            if [ $1 -eq 0 ]; then
                    error "First param must be greater than 0!"
            else
                    echo "OK!"
            fi
    }

    RESULT=`do_sth 0`

    echo "This line should never be printed"

如何立即终止error()函数中的脚本?

4

5 回答 5

4

命令替换的问题是,子shell 开始执行do_sthexit 1然后终止这个子shell,而不是主bash。

您可以通过附加 来解决此问题|| exit $?,它使用命令替换的退出代码退出

RESULT=`do_sth 0` || exit $?

如果要显示错误消息,请将其重定向到 stderr

echo "An error has occured: $1" >&2
于 2013-01-17T12:15:34.400 回答
1
RESULT=`do_sth 0` || exit $?

然后回显“发生错误:$1”>&2

于 2013-01-17T14:38:00.040 回答
0

没有办法避免子shell 不能使父进程直接终止这一事实:父进程必须评估从子shell 返回的值。一种常见的技术是陷阱退出并在陷阱函数中打印错误消息。由于您在子 shell 中生成错误消息,因此您不能像其他方式那样简单地将消息分配给变量,但您可以使用文件系统。请注意,这个想法真的很傻,简单地将错误消息写入stderr要干净得多。这就是它的用途,这就是它被孩子继承的原因。就像是:

#!/bin/sh

trap final 0 2 15

# Create a temporary file to store error messages.  This is a terrible
# idea: it would be much better to simply write error messages to stderr,
# but this code is attempting to demonstrate the technique of having the
# parent print the message.  Perhaps it would do better to serve as an example
# of why reporting your children's mistakes is a bad idea.  The children
# should be responsible for reporting their own errors.  Doing so is easy,
# since they inherit file descriptor 2 from their parent.
errmsg=$( mktemp xxxxx )
final() {
    test "$?" = 0 || cat $errmsg
    rm -f $errmsg
} >&2

# Must emphasize one more time that this is a silly idea.  The error 
# function ought to be writing to stderr: eg echo "error: $*" >&2
error() { echo "error: $*" > $errmsg; exit 1; }
do_sth() { 
    if test "$1" -eq 0; then
        error "First param must be greater than 0!"
    else
        echo "OK!"
    fi
}


result=$( do_sth 0 ) || exit 1
echo not printed
于 2013-01-17T13:30:36.070 回答
0

似乎(聪明的)答案是@FatalError的另一个问题的最佳答案,在这里

于 2013-01-17T14:05:55.613 回答
0

这应该工作...

#!/bin/bash

trap "exit 1" 50                                       #exit process after receiving signal 50.

function myerror ()
{
    echo "An error has occured: $1" >&2
}
function do_sth ()
{
    if [ $1 -eq 0 ]; then
        myerror "First param must be greater than 0!"
        kill -50 $(ps --pid $$ -opid=)                 #uncommon signal 50 is used.
    else
        echo "OK!"
    fi
}

RESULT=`do_sth 1`
echo $RESULT

RESULT=`do_sth 0`
echo $RESULT

echo "This line should never be printed"
于 2013-01-17T14:56:22.520 回答