如何获得“ set -e”的效果和有用性?壳函数里面? [英] How do I get the effect and usefulness of "set -e" inside a shell function?

查看:88
本文介绍了如何获得“ set -e”的效果和有用性?壳函数里面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

set -e (或以#!/ bin / sh -e 开头的脚本)非常有用如果有问题可以自动炸开。

set -e (or a script starting with #!/bin/sh -e) is extremely useful to automatically bomb out if there is a problem. It saves me having to error check every single command that might fail.

如何在函数中获取与之等效的结果?

How do I get the equivalent of this inside a function?

例如,我有以下脚本会在出现错误时立即退出,并显示错误退出状态:

For example, I have the following script that exits immediately on error with an error exit status:

#!/bin/sh -e

echo "the following command could fail:"
false
echo "this is after the command that fails"

输出符合预期:

the following command could fail:

现在我想将其包装到一个函数中:

Now I'd like to wrap this into a function:

#!/bin/sh -e

my_function() {
    echo "the following command could fail:"
    false
    echo "this is after the command that fails"
}

if ! my_function; then
    echo "dealing with the problem"
fi

echo "run this all the time regardless of the success of my_function"

预期的输出:

the following command could fail:
dealing with the problem
run this all the time regardless of the success of my_function



是否成功

实际输出:

Actual output:

the following output could fail:
this is after the command that fails
run this all the time regardless of the success of my_function

是否成功(即该函数忽略 set -e

(ie. the function is ignoring set -e)

这大概是预期的行为。我的问题是:如何在shell函数中获得 set -e 的效果和有用性?我希望能够进行一些设置,以使我不必分别对每个调用进行错误检查,但是脚本会在遇到错误时停止运行。在我检查结果之前,它应该尽可能地展开堆栈,如果我没有检查结果,则退出脚本本身。这是 set -e 所做的,除了它不嵌套。

This presumably is expected behaviour. My question is: how do I get the effect and usefulness of set -e inside a shell function? I'd like to be able to set something up such that I don't have to individually error check every call, but the script will stop on encountering an error. It should unwind the stack as far as is needed until I do check the result, or exit the script itself if I haven't checked it. This is what set -e does already, except it doesn't nest.

我发现同一问题在堆栈溢出外部提出

I've found the same question asked outside Stack Overflow but no suitable answer.

推荐答案

来自 set -e 的文档:


启用此选项后,如果简单命令由于
的后果中列出的
中的任何一个原因而失败Shell错误或返回退出状态
值> 0,并且不在 while
<$之后的
复合列表中c $ c>直到或 if 关键字,并且不是 AND的
部分
OR 列表,而不是
a管道之前是保留的
一词,然后外壳程序应立即退出

When this option is on, if a simple command fails for any of the reasons listed in Consequences of Shell Errors or returns an exit status value > 0, and is not part of the compound list following a while, until, or if keyword, and is not a part of an AND or OR list, and is not a pipeline preceded by the ! reserved word, then the shell shall immediately exit.

在您的情况下, false 是一个pa 的一部分位于 if 之前。因此,解决方案是重写代码,而不是重写代码。

In your case, false is a part of a pipeline preceded by ! and a part of if. So the solution is to rewrite your code so that it isn't.

换句话说,这里的函数没有什么特别的。尝试:

In other words, there's nothing special about functions here. Try:

set -e
! { false; echo hi; }

这篇关于如何获得“ set -e”的效果和有用性?壳函数里面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆