在 bash 中添加(收集)退出代码

发布于 2024-07-25 12:13:27 字数 388 浏览 4 评论 0原文

我需要依赖于脚本中的几个单独的执行,并且不想将它们全部捆绑在一个丑陋的“if”语句中。 我想使用退出代码“$?” 每个执行并添加它; 最后,如果这个值超过阈值 - 我想执行一个命令。

伪代码:

ALLOWEDERROR=5

run_something
RESULT=$?
..other things..

run_something_else
RESULT=$RESULT + $?

if [ $RESULT -gt ALLOWEDERROR ] 
   then echo "Too many errors"
fi

问题:即使互联网另有说法,bash 也拒绝处理 RESULT 和 $? 作为整数。 正确的语法是什么?

谢谢。

I need to depend on few separate executions in a script and don't want to bundle them all in an ugly 'if' statement. I would like to take the exit code '$?' of each execution and add it; at the end, if this value is over a threshold - I would like to execute a command.

Pseudo code:

ALLOWEDERROR=5

run_something
RESULT=$?
..other things..

run_something_else
RESULT=$RESULT + $?

if [ $RESULT -gt ALLOWEDERROR ] 
   then echo "Too many errors"
fi

Issue: Even though the Internet claims otherwise, bash refuses to treat the RESULT and $? as integer. What is the correct syntax?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

鹊巢 2024-08-01 12:13:27

一个快速实验并深入研究 bash 信息表明:

declare -i RESULT=$RESULT + $?

由于您多次添加到结果中,因此您可以在开始时使用声明,如下所示:

declare -i RESULT=0

true
RESULT+=$?
false
RESULT+=$?
false
RESULT+=$?

echo $RESULT
2

这看起来更干净。

declare -i 表示变量是整数。

或者,您可以避免声明并使用算术表达式括号:

RESULT=$(($RESULT+$?))

A quick experiment and dip into bash info says:

declare -i RESULT=$RESULT + $?

since you are adding to the result several times, you can use declare at the start, like this:

declare -i RESULT=0

true
RESULT+=$?
false
RESULT+=$?
false
RESULT+=$?

echo $RESULT
2

which looks much cleaner.

declare -i says that the variable is integer.

Alternatively you can avoid declare and use arithmetic expression brackets:

RESULT=$(($RESULT+$?))
罪歌 2024-08-01 12:13:27

您可能想看看内置的 trap 看看它是否有帮助:

help trap

或者

man bash

您可以为错误设置一个陷阱,如下所示:

#!/bin/bash

AllowedError=5

SomeErrorHandler () {
    (( errcount++ ))       # or (( errcount += $? ))
    if  (( errcount > $AllowedError ))
    then
        echo "Too many errors"
        exit $errcount
    fi
}

trap SomeErrorHandler ERR

for i in {1..6}
do
    false
    echo "Reached $i"     # "Reached 6" is never printed
done

echo "completed"          # this is never printed

如果您对错误进行计数(并且仅当它们是错误时)像这样而不是使用“$?”,那么您不必担心返回值不是零或一。 例如,单个返回值 127 会立即使您超过阈值。 除了ERR之外,您还可以为其他信号注册trap

You might want to take a look at the trap builtin to see if it would be helpful:

help trap

or

man bash

you can set a trap for errors like this:

#!/bin/bash

AllowedError=5

SomeErrorHandler () {
    (( errcount++ ))       # or (( errcount += $? ))
    if  (( errcount > $AllowedError ))
    then
        echo "Too many errors"
        exit $errcount
    fi
}

trap SomeErrorHandler ERR

for i in {1..6}
do
    false
    echo "Reached $i"     # "Reached 6" is never printed
done

echo "completed"          # this is never printed

If you count the errors (and only when they are errors) like this instead of using "$?", then you don't have to worry about return values that are other than zero or one. A single return value of 127, for example, would throw you over your threshold immediately. You can also register traps for other signals in addition to ERR.

与君绝 2024-08-01 12:13:27

使用 $(( ... )) 构造。

$ cat st.sh
RESULT=0
true
RESULT=$(($RESULT + $?))
false
RESULT=$(($RESULT + $?))
false
RESULT=$(($RESULT + $?))
echo $RESULT
$ sh st.sh
2
$

Use the $(( ... )) construct.

$ cat st.sh
RESULT=0
true
RESULT=$(($RESULT + $?))
false
RESULT=$(($RESULT + $?))
false
RESULT=$(($RESULT + $?))
echo $RESULT
$ sh st.sh
2
$
氛圍 2024-08-01 12:13:27

正如 mouviciel 提到的,收集返回代码的总和看起来相当毫无意义。 也许,您可以使用数组来累积非零结果代码并检查其长度。 这种方法的示例如下:

#!/bin/sh

declare RESULT
declare index=0
declare ALLOWED_ERROR=1

function write_result {
    if [ $1 -gt 0 ]; then
        RESULT[index++]=$1
    fi
}

true
write_result $?

false
write_result $?

false
write_result $?

echo ${#RESULT[*]}
if [ ${#RESULT[*]} -gt $ALLOWEDERROR ] 
   then echo "Too many errors"
fi

As mouviciel mentioned collecting sum of return codes looks rather senseless. Probably, you can use array for accumulating non-zero result codes and check against its length. Example of this approach is below:

#!/bin/sh

declare RESULT
declare index=0
declare ALLOWED_ERROR=1

function write_result {
    if [ $1 -gt 0 ]; then
        RESULT[index++]=$1
    fi
}

true
write_result $?

false
write_result $?

false
write_result $?

echo ${#RESULT[*]}
if [ ${#RESULT[*]} -gt $ALLOWEDERROR ] 
   then echo "Too many errors"
fi
七色彩虹 2024-08-01 12:13:27

有关如何在 Bash 中添加数字另请参阅:

help let 

For how to add numbers in Bash also see:

help let 
断念 2024-08-01 12:13:27

如果要在脚本中使用 ALLOWEDERROR,请在其前面加上 $,例如 $ALLOWEDERROR。

If you want to use ALLOWEDERROR in your script, preface it with a $, e.g $ALLOWEDERROR.

娇纵 2024-08-01 12:13:27

以下是在 bash 或 sh 中执行加法的一些方法:

RESULT=`expr $RESULT + $?`
RESULT=`dc -e "$RESULT $? + pq"`

以及仅在 bash 中的其他一些方法:

RESULT=$((RESULT + $?))
RESULT=`bc <<< "$RESULT + $?"` 

无论如何,错误时的退出状态并不总是 1 并且其值不取决于错误级别,因此在一般情况下没有太大意义根据阈值检查状态总和。

Here are some ways to perform an addition in bash or sh:

RESULT=`expr $RESULT + $?`
RESULT=`dc -e "$RESULT $? + pq"`

And some others in bash only:

RESULT=$((RESULT + $?))
RESULT=`bc <<< "$RESULT + $?"` 

Anyway, exit status on error is not always 1 and its value does not depend on error level, so in the general case there is not much sense to check a sum of statuses against a threshold.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文