set -e 在 i=0 处退出;let i++
仅当变量的先前值为零时,以下带有调试选项 set -e -v
的脚本在增量运算符处失败。
#!/bin/bash
set -e -v
i=1; let i++; echo "I am still here"
i=0; let i++; echo "I am still here"
i=0; ((i++)); echo "I am still here"
bash (GNU bash,版本 4.0.33(1)-release (x86_64-apple-darwin10) 还有 GNU bash,版本 4.2.4(1)-release (x86_64-unknown-linux-gnu))
有什么想法吗?
The following script with debug option set -e -v
fails at the increment operator only when the variable has a prior value of zero.
#!/bin/bash
set -e -v
i=1; let i++; echo "I am still here"
i=0; let i++; echo "I am still here"
i=0; ((i++)); echo "I am still here"
bash (GNU bash, version 4.0.33(1)-release (x86_64-apple-darwin10) but also GNU bash, version 4.2.4(1)-release (x86_64-unknown-linux-gnu))
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的问题的答案不是使用 let (或 shift,或...),而是
在尝试通过设置“exit”来检查 bash 脚本时 使用bash手册
指出,set -e 具有“如果简单命令以非零状态退出,则立即退出”的效果。< /强>'。
不幸的是 let (以及 shift 和 ...)返回计算结果('如果最后一个参数计算结果为 0,let 返回 1;返回 0否则')。因此,我们得到的是某种返回值,而不是状态代码。有时这个返回值为零,有时为一,具体取决于计算。因此 set -e 将导致脚本退出,具体取决于您的计算结果!并且没有什么可做的,除非您永远不使用它或诉诸
arnaud576875 指出的方法,顺便说一句,这会增加额外的 CPU 负担。
使用
仅适用于 i 不为 -1 的特定情况,就像 let i++ 一样,它仅适用于 i 不为 0 时。因此是半解。
虽然我喜欢 Unix,但我别无选择。
the answer to my question is not to use let (or shift, or...) but to use
when trying to check a bash script by setting 'exit on non-zero status code' with
The bash manual states that set -e has the effect of 'Exit immediately if a simple command exits with a non-zero status.'.
Unfortunately let (and shift and ...) return the result of the computation ('If the last arg evaluates to 0, let returns 1; 0 is returned otherwise'). So instead of a status code one gets a return value of some sort. And sometimes this return value will be zero and sometimes one depending on the computation. Therefore set -e will cause the script to exit depending on the result of your computation!!! and there is nothing to do about it unless either you don't use it ever or resort to
as pointed by arnaud576875 which btw adds extra CPU burden.
Using
works only for the specific case that i is not -1, as with let i++ which works only for when i is not 0. Therefore half-solutions.
I love Unix though, I wouldn't have it any other way.
如果
let
的最后一个参数的计算结果为0
,let 返回1
(因此,非零状态):当
i
为0
时,i++
的计算结果为零(因为它是后置增量,所以i
的先前值返回),因此let
返回1
,并且由于set -e
,bash 存在。以下是一些解决方案:
If the last argument of
let
evaluates to0
, let returns1
(so, a non-zero status):i++
evaluates to zero wheni
is0
(because it's a post-increment, so the previous value ofi
is returned), solet
returns1
, and due toset -e
, bash exists.Here are some solutions:
查看
set -e
上的 BASH 手册页:因此,如果任何语句返回非零退出代码,shell 将退出。
查看 BASH 手册页,在
let
上命令:但是等等!
i++
的答案是一而不是零!应该有用的!同样,答案是关于增量运算符的 BASH 联机帮助页:
好吧,不太清楚。试试这个 shell 脚本:
嗯...它按预期工作,我所做的就是将每一行中的
i++
更改为++i
。i++
是一个后递增运算符。这意味着,let
语句返回值后, 会递增i
。由于i
在递增之前为零,因此let
语句返回一个非零值。然而,
++i
是一个预增量运算符。这意味着它在返回退出状态之前递增i
。由于i
递增到1
,退出状态变为零。Looking at the BASH manpage on the
set -e
:So, if any statement returns a non-zero exit code, the shell will exit.
Taking a look at the BASH manpage, on the
let
command:But wait! The answer to
i++
is a one and not a zero! It should have worked!Again, the answer is with the BASH manpage on the increment operator:
Okay, not so clear. Try this shell script:
Hmmm... that works as expected, and all I did was change
i++
to++i
in each line.The
i++
is a post-increment operator. That means, it incrementsi
after thelet
statement returns a value. Sincei
was zero before being incremented, thelet
statement returns a non-zero value.However, the
++i
is a pre-increment operator. That means it incrementsi
before returning the exit status. Sincei
is incremented to a1
, the exit status becomes a zero.