shell脚本中特定exit_code的异常处理?

pzfprimi  于 2023-11-21  发布在  Shell
关注(0)|答案(7)|浏览(179)

我明白

a_command || fallback_command

字符串
它会从command_a中捕获任何非零的exit_code(即当command_a失败时),并运行fallback_commad。如何为某些特定代码实现这一点?如何使它像a_command抛出255时那样工作,然后运行fallback_command?
我试着做一些事情,

a_command
VAR=$?
if [ $VAR==255 ]
then
    fallback_command
    exit 0
fi


但是这不会抑制由command_a抛出的非零错误。我想抑制非零的exit_codes。

mm5n2pyu

mm5n2pyu1#

IMHO case是完美的这一点

a_command
VAR=$?
case $VAR in
    255) fallback_command ;;
    123) fallback_command2;;
    ...) ...   ;;
      0) exit 0;;
esac

字符串

k4ymrczo

k4ymrczo2#

就像这样简单地维护内联布尔或||

a_command || [ $? -ne 255 ] || fallback_command

字符串
测试实现:

#!/usr/bin/env bash

a_command_success(){ echo 'running a_command_success';}
a_command_failing_42(){ echo 'running a_command_failing_42'; return 42;}
a_command_failing_255(){ echo 'running a_command_failing_255'; return 255;}

fallback_command(){ echo 'running fallback_command';}

for a_command in a_command_success a_command_failing_255 a_command_failing_42
do "$a_command" || [ $? -ne 255 ] || fallback_command; done


测试输出:

running a_command_success
running a_command_failing_255
running fallback_command
running a_command_failing_42


进一步使用fall_back caller实用程序命令:

# When return code equals argument 1, execute command and arguments 2+
# Example:
# a_command || falllback_rc 255 fallback_command arg1 arg2 ... argn
fallback_rc(){ [ "$?" -eq "$1" ]&&{ shift;"$@";}}

wbrvyc0a

wbrvyc0a3#

您的代码中有一个错误。[ $VAR==255 ]将始终为真,因为它与下面的代码相同:

[ string ]
Returns true if string is not empty.

字符串
如果要比较数字,请使用-eq

if [ $VAR -eq 255 ]


同样,若要测试字串的相等性,您可以用途:

if [ $VAR = 255 ]


在本例中,您希望使用-eq
此页面可以帮助:https://www.computerhope.com/unix/bash/test.htm

hs1ihplo

hs1ihplo4#

你可以使用if指令:

command_a

if test $? = 255

then

   fallback_command

fi

字符串
一条线:

command_a ; if test $? = 255 ; then fallback_command ; fi

k7fdbhmy

k7fdbhmy5#

您的代码将始终执行fallback_command。原因是如果x不为空,则test x总是成功。例如,假设VAR等于100,因此您基本上执行test 100==255,并且由于字符串 100==255 不为空(它的长度为8个字符),因此条件成功。
你可以这样做:

if (( VAR > 127 ))
then
  # There was a problem starting the program
  ....
elif (( VAR == 72 || VAR >118 ))
then
  # Deal with exit codes 72 and 119 up to 127
  .....
elif (( VAR > 0 ))
  # Deal with all other non-zero exit codes
  ....
fi

字符串

hgc7kmma

hgc7kmma6#

嗯,在尝试了一点之后,我发现了一种方法,

a_command || export ECODE="$?"    # Here non zero exit codes are suppressed.

if [ "$ECODE" -eq 255 ]

then
    fallback_command
fi

字符串

13z8s7eq

13z8s7eq7#

使用set -e和陷阱:

set -e

trap cleanup ERR

cleanup () {
    echo cleaning up
}

a_command && rc=0 || rc=$?
case $rc in
    0)
        # no error handling needed
        ;;

    255)
        # handle error code 255
        fallback_command
        ;;
    254)
        # handle error code 254
        another_fallback_command
        ;;
    *)
        # unhandled error code; subshell used so we can trigger `set -e`
        # and any associated traps without exiting directly
        (exit "$rc")
        ;;
esac

字符串
如果没有使用ERR上的陷阱,我们可以像set -e一样直接退出:

set -e

a_command && rc=0 || rc=$?
case $rc in
    0)
        # no error handling needed
        ;;

    255)
        # handle error code 255
        fallback_command
        ;;
    254)
        # handle error code 254
        another_fallback_command
        ;;
    *)
        # unhandled error code; exit like `set -e` normally would
        exit "$rc"
        ;;
esac

相关问题