在bash中使用set -e / set + e与功能

米歇尔·米勒

我在脚本中一直使用像这样的简单bash序言:

#!/bin/bash
set -e

结合模块化/使用功能,这今天困扰了我。

所以,说我有一个类似的功能

foo() {
  #edit: some error happens that make me want to exit the function and signal that to the caller 
  return 2
}

理想情况下,我希望能够使用多个小文件,将它们的功能包含在其他文件中,然后像

set +e
foo
rc=$?
set -e

这恰好适用于两层例程。但是,如果foo也这样调用子例程,则返回之前的最后一个设置将是set -e,这将使脚本在返回时退出-我无法在调用函数中覆盖此函数。所以,我要做的是

foo() {
  #calling bar() in a shielded way like above
  #..      

  set +e
  return 2
}

我发现这很违反直觉(也不是我想要的)-如果在某些情况下我想使用该函数而不屏蔽故障,而在其他情况下我想处理清理该怎么办?)什么是最好的处理方式? ?顺便说一句。我正在OSX上执行此操作,尚未测试Linux上的这种行为是否有所不同。

马丁·图尔诺伊(Martin Tournoij)

Shell函数实际上并没有“返回值”,只是退出代码。

您可以将其添加&& :到调用方,这会使命令“经过测试”,并且不会退出该命令:

foo() {
    echo 'x'
    return 42
}

out=$(foo && :)
echo $out

:是“空指令”(即它没有做任何事情)。在这种情况下,它甚至不会被执行,因为它仅在foo返回0时才运行(不是)。

输出:

x

可以说这有点丑陋,但是话又说回来,所有的shell脚本都可以说有点丑陋;-)

引用sh(1)FreeBSD,它比bash的手册页更好地解释了这一点:

 -e errexit
         Exit immediately if any untested command fails in non-interactive
         mode.  The exit status of a command is considered to be explicitly
         tested if the command is part of the list used to control an if,
         elif, while, or until; if the command is the left hand operand of
         an “&&” or “||” operator; or if the command is a pipeline preceded
         by the ! operator.  If a shell function is executed and its exit
         status is explicitly tested, all commands of the function are con‐
         sidered to be tested as well.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Bash“ set -e”的功能是什么

来自分类Dev

在Bash中使用'set -e'时如何捕获ERR

来自分类Dev

如何在bash命令替换中使用`set -e`?

来自分类Dev

bash set -e无效

来自分类Dev

bash:在if-expression中使用时,set -e不起作用?

来自分类Dev

键入`set -e`杀死bash

来自分类Dev

bash函数中的`set -e`

来自分类Dev

bash set -e函数与双管道

来自分类Dev

暂时在bash中禁用'set-e'/'set -o errexit'?

来自分类Dev

在bash中set -e和set -a有什么作用?set命令可以使用哪些其他选项

来自分类Dev

Meaning of '@' in "@set -e" command in Makefiles

来自分类Dev

如果bash脚本由于set -e而终止,则显示错误消息

来自分类Dev

通过自省查找通用类型的Iterable(Set <E>,List <E>,...)

来自分类Dev

set -e和set -o errexit之间有区别吗?

来自分类Dev

Makefile中“ @set -e”命令中“ @”的含义

来自分类Dev

set -e的作用是什么?

来自分类Dev

set -e的作用是什么?

来自分类Dev

如何禁用单个命令的set -e?

来自分类Dev

Android-Set e Get Conversion

来自分类Dev

使用set -e,是否可以忽略某些命令的错误?

来自分类Dev

“ set -e”如何与子外壳一起使用?

来自分类Dev

如果存在set -e指令,为什么((0))导致Bash脚本退出?

来自分类Dev

鱼壳:如何在出错时退出(bash set -e)

来自分类Dev

具有`set -e`的Bash脚本不会在`... && ...`命令上停止

来自分类Dev

尽管set -e和/或trap处于活动状态,但未捕获Bash退出状态

来自分类Dev

程序以状态!= 0(set -e)退出后执行EXIT陷阱时的Bash函数作用域状态

来自分类Dev

bash的set -e的cmd.exe替代品是什么?

来自分类Dev

具有`set -e`的Bash脚本不会在`... && ...`命令上停止

来自分类Dev

set -e命令在Unix中有什么作用?

Related 相关文章

  1. 1

    Bash“ set -e”的功能是什么

  2. 2

    在Bash中使用'set -e'时如何捕获ERR

  3. 3

    如何在bash命令替换中使用`set -e`?

  4. 4

    bash set -e无效

  5. 5

    bash:在if-expression中使用时,set -e不起作用?

  6. 6

    键入`set -e`杀死bash

  7. 7

    bash函数中的`set -e`

  8. 8

    bash set -e函数与双管道

  9. 9

    暂时在bash中禁用'set-e'/'set -o errexit'?

  10. 10

    在bash中set -e和set -a有什么作用?set命令可以使用哪些其他选项

  11. 11

    Meaning of '@' in "@set -e" command in Makefiles

  12. 12

    如果bash脚本由于set -e而终止,则显示错误消息

  13. 13

    通过自省查找通用类型的Iterable(Set <E>,List <E>,...)

  14. 14

    set -e和set -o errexit之间有区别吗?

  15. 15

    Makefile中“ @set -e”命令中“ @”的含义

  16. 16

    set -e的作用是什么?

  17. 17

    set -e的作用是什么?

  18. 18

    如何禁用单个命令的set -e?

  19. 19

    Android-Set e Get Conversion

  20. 20

    使用set -e,是否可以忽略某些命令的错误?

  21. 21

    “ set -e”如何与子外壳一起使用?

  22. 22

    如果存在set -e指令,为什么((0))导致Bash脚本退出?

  23. 23

    鱼壳:如何在出错时退出(bash set -e)

  24. 24

    具有`set -e`的Bash脚本不会在`... && ...`命令上停止

  25. 25

    尽管set -e和/或trap处于活动状态,但未捕获Bash退出状态

  26. 26

    程序以状态!= 0(set -e)退出后执行EXIT陷阱时的Bash函数作用域状态

  27. 27

    bash的set -e的cmd.exe替代品是什么?

  28. 28

    具有`set -e`的Bash脚本不会在`... && ...`命令上停止

  29. 29

    set -e命令在Unix中有什么作用?

热门标签

归档