[英]Bash post increment aborts script execution
Running with bash -e
:使用
bash -e
运行:
round=0
((round++))
echo "Done"
Will not show Done
.不会显示
Done
。 Why?为什么? How can I use post-increment when
-e
is set?设置
-e
时如何使用后增量?
Please take a look of some examples:请看一些例子:
round=0
((round++))
echo $?
round=1
((round-1))
echo $?
((0))
echo $?
In all cases $?
在所有情况下
$?
returns 1.返回 1。
Bash
manpage states: Bash
联机帮助页指出:
((expression))
((表达))
The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION.表达式根据以下算术求值中描述的规则进行求值。 If the value of the expression is non-zero, the return status is 0;
如果表达式的值非零,则返回状态为 0; otherwise the return status is 1. This is exactly equivalent to let "expression".
否则返回状态为 1。这完全等同于 let "expression"。
If the -e
option is set, then the script exits because:如果设置了
-e
选项,则脚本将退出,因为:
-e
-e
Exit immediately if a pipeline (which may consist of a single simple command), a list, or a compound command (see SHELL GRAMMAR above), exits with a non-zero status.如果管道(可能由单个简单命令组成)、列表或复合命令(参见上面的 SHELL GRAMMAR)以非零状态退出,则立即退出。 ...
...
You can avoid the termination by assigning a variable to the result of arithmetic evaluation:您可以通过为算术计算结果分配一个变量来避免终止:
set -e
round=0
dummy=$((round++))
echo "Done"
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.