在 bash 中添加(收集)退出代码
我需要依赖于脚本中的几个单独的执行,并且不想将它们全部捆绑在一个丑陋的“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
一个快速实验并深入研究 bash 信息表明:
由于您多次添加到结果中,因此您可以在开始时使用声明,如下所示:
这看起来更干净。
declare -i
表示变量是整数。或者,您可以避免声明并使用算术表达式括号:
A quick experiment and dip into bash info says:
since you are adding to the result several times, you can use declare at the start, like this:
which looks much cleaner.
declare -i
says that the variable is integer.Alternatively you can avoid declare and use arithmetic expression brackets:
您可能想看看内置的
trap
看看它是否有帮助:或者
您可以为错误设置一个陷阱,如下所示:
如果您对错误进行计数(并且仅当它们是错误时)像这样而不是使用“
$?
”,那么您不必担心返回值不是零或一。 例如,单个返回值 127 会立即使您超过阈值。 除了ERR
之外,您还可以为其他信号注册trap
。You might want to take a look at the
trap
builtin to see if it would be helpful:or
you can set a trap for errors like this:
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 registertrap
s for other signals in addition toERR
.使用
$(( ... ))
构造。Use the
$(( ... ))
construct.正如 mouviciel 提到的,收集返回代码的总和看起来相当毫无意义。 也许,您可以使用数组来累积非零结果代码并检查其长度。 这种方法的示例如下:
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:
有关如何在 Bash 中添加数字另请参阅:
For how to add numbers in Bash also see:
如果要在脚本中使用 ALLOWEDERROR,请在其前面加上 $,例如 $ALLOWEDERROR。
If you want to use ALLOWEDERROR in your script, preface it with a $, e.g $ALLOWEDERROR.
以下是在 bash 或 sh 中执行加法的一些方法:
以及仅在 bash 中的其他一些方法:
无论如何,错误时的退出状态并不总是 1 并且其值不取决于错误级别,因此在一般情况下没有太大意义根据阈值检查状态总和。
Here are some ways to perform an addition in bash or sh:
And some others in bash only:
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.