递增变量会触发bash 4中的EXIT,但不会触发bash 3中的EXIT

前端之家收集整理的这篇文章主要介绍了递增变量会触发bash 4中的EXIT,但不会触发bash 3中的EXIT前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
考虑这个(示例性)bash脚本:
  1. #!/bin/bash -e
  2. errorExit() {
  3. echo "" >&2
  4. echo "ERROR (${var_scriptfilename}):" >&2
  5. echo "An unhandled error occurred." >&2
  6. intentionalExit 1
  7. }
  8. intentionalExit () {
  9. trap - EXIT # Unregister the EXIT trap
  10. exit $1
  11. }
  12. trap errorExit EXIT # Trap script errors
  13. var_scriptfilename="$(basename "$0")"
  14. # ==== START OF TEST ====
  15. var_counter=0
  16. ((var_counter++))
  17. echo "var_counter is $var_counter" >&2
  18. # ===== END OF TEST =====
  19. intentionalExit 0

如果我在Cygwin的bash中运行它会产生预期的输出

  1. var_counter is 1

但是,如果我在我的Debian Squeeze盒子上运行它,这是它的预定目的地,我最终进入了EXIT陷阱:

  1. ERROR (test.increment.sh):
  2. An unhandled error occurred.

…这是为什么?

如果我删除了-e选项,它在两个系统上都按预期工作,但显然我想保持-e使用.

更加繁琐的“通用”变体var_counter = $(($var_counter 1)),在两个shell上设置了-e,但是我更喜欢使用第一个符号(或类似的东西),因为它明显地粘住了在读取代码时作为增量操作.

bash – 在Cygwin bash上的版本说:

  1. GNU bash,version 3.2.51(24)-release (i686-pc-cygwin)
  2. Copyright (C) 2007 Free Software Foundation,Inc.

在Debian上,它是:

  1. GNU bash,Version 4.1.5(1)-release (x86_64-pc-linux-gnu)
  2. Copyright (C) 2009 Free Software Foundation,Inc.

我很感兴趣为什么会这样.有人知道这种行为的原因吗?

另外,是否有人知道在bash中增加变量的类似方法我可以使用?

来自Debian的bash4手册页:
  1. ((expression))
  2. The expression is evaluated according to the rules described
  3. below under ARITHMETIC EVALUATION. If the value of the expres
  4. sion is non-zero,the return status is 0; otherwise the return
  5. status is 1. This is exactly equivalent to let "expression".

并且 …

  1. -e Exit immediately if a pipeline (which may consist of a
  2. single simple command),a subshell command enclosed in
  3. parentheses,or one of the commands executed as part of
  4. a command list enclosed by braces (see SHELL GRAMMAR
  5. above) exits with a non-zero status.

所以正在发生的是((var))将var从0增加到1并返回
0,导致整个表达式返回非零,从而触发
errexit.

现在来看两个不同的bash版本之间的区别:这个改变
in((行为似乎发生在4.0和4.1之间.在4.0中((
显然没有触发errexit.有关详细信息,请参阅此NEWS文件.
你必须向下滚动到135行左右.来自源的Changelog
分配似乎证实了这一点.

如果您只想在不使用退出状态的情况下增加变量,
有多种方法可以做到这一点.也许其他人可以给出建议
哪个是最好的,但有些可能性是:

> var =“$((var 1))”,便携式POSIX sh方法>((var))|| true,强制语句始终为零退出状态(仅限bash)

猜你在找的Bash相关文章