在 bash 中添加(收集)退出代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1000370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add (collect) exit codes in bash
提问by mik
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.
我需要依赖脚本中的几个单独执行,并且不想将它们全部捆绑在一个丑陋的“if”语句中。我想要退出代码“$?” 每次执行并添加它;最后,如果该值超过阈值 - 我想执行一个命令。
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?
问题:即使互联网上另有说法,bash 也拒绝处理 RESULT 和 $? 作为整数。什么是正确的语法?
Thanks.
谢谢。
回答by Alex Brown
A quick experiment and dip into bash info says:
一个快速实验并深入了解 bash 信息说:
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 -isays that the variable is integer.
declare -i表示变量是整数。
Alternatively you can avoid declare and use arithmetic expression brackets:
或者,您可以避免声明和使用算术表达式括号:
RESULT=$(($RESULT+$?))
回答by Paused until further notice.
You might want to take a look at the trapbuiltin to see if it would be helpful:
您可能想看看trap内置函数,看看它是否有帮助:
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.
如果您像这样计算错误(并且仅当它们是错误时)而不是使用“ $?”,那么您不必担心返回值不是零或一。例如,单个返回值 127 会立即使您超过阈值。trap除了 之外,您还可以为其他信号注册s ERR。
回答by mik
For how to add numbers in Bash also see:
有关如何在 Bash 中添加数字的信息,请参阅:
help let
回答by xcramps
If you want to use ALLOWEDERROR in your script, preface it with a $, e.g $ALLOWEDERROR.
如果您想在脚本中使用 ALLOWEDERROR,请以 $ 开头,例如 $ALLOWEDERROR。
回答by dave4420
Use the $(( ... ))construct.
使用$(( ... ))构造。
$ cat st.sh
RESULT=0
true
RESULT=$(($RESULT + $?))
false
RESULT=$(($RESULT + $?))
false
RESULT=$(($RESULT + $?))
echo $RESULT
$ sh st.sh
2
$
回答by mouviciel
Here are some ways to perform an addition in bash or sh:
以下是在 bash 或 sh 中执行加法的一些方法:
RESULT=`expr $RESULT + $?`
RESULT=`dc -e "$RESULT $? + pq"`
And some others in bash only:
还有一些只在 bash 中:
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.
无论如何,错误退出状态并不总是 1 并且其值不取决于错误级别,因此在一般情况下,根据阈值检查状态总和没有多大意义。
回答by Rorick
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:
正如 mouviciel 提到的,收集返回码的总和看起来毫无意义。也许,您可以使用数组来累积非零结果代码并检查其长度。这种方法的示例如下:
#!/bin/sh
declare RESULT
declare index=0
declare ALLOWED_ERROR=1
function write_result {
if [ -gt 0 ]; then
RESULT[index++]=
fi
}
true
write_result $?
false
write_result $?
false
write_result $?
echo ${#RESULT[*]}
if [ ${#RESULT[*]} -gt $ALLOWEDERROR ]
then echo "Too many errors"
fi

