什么`在shell脚本设置-o errtrace`将执行? [英] What `set -o errtrace` in shell script will perform?

查看:140
本文介绍了什么`在shell脚本设置-o errtrace`将执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 设置-o errtrace

在什么shell脚本上述声明将执行?
具体来说 -o 命令?


解决方案

这本手册:


  errtrace
        同-E。-E如果设置,在任何ERR陷阱被shell函数继承,
        命令替换和命令在执行分
        shell环境。该ERR陷阱通常不是固有的
        资讯科技教育在这种情况下。


启用 errtrace ,当错误(返回非零code命令),函数或子shell内发生,也引发了ERR陷阱。我们大概可以讲的是在一个函数或上下文子shell不继承ERR陷阱换句话说,除非 errtrace 已启用。

 #!/斌/庆典
设置-o errtrace
函数x {
    回声X起始
    假
    回声X最终
}
函数y {
    回声是开始
    假
    回声Y终点
}
陷阱回声发生在$ FUNCNAME错误。ERR
X
ÿ

真正

输出:

  X启动
发生在X误差。
X最终
是开始
发生在Y误差。
Y终点
发生错误。

errtrace 未启用

  X启动
X最终
是开始
Y终点
发生错误。

set -o errtrace

What the above statement in shell script will perform? Specifically -o command?

解决方案

From the manual:

errtrace
        Same as -E.

-E      If set, any trap on ERR is inherited by shell functions,
        command substitutions, and commands executed in a sub‐
        shell environment.  The ERR trap is normally not inher‐
        ited in such cases.

When errtrace is enabled, the ERR trap is also triggered when the error (a command returning a nonzero code) occurs inside a function or a subshell. We can probably tell is in other words that the context of a function or a subshell does not inherit the ERR trap unless the errtrace is enabled.

#!/bin/bash
set -o errtrace
function x {
    echo x start
    false
    echo x end
}
function y {
    echo y start
    false
    echo y end
}   
trap 'echo "Error occurred on $FUNCNAME."' ERR
x
y
false
true

Output:

x start
Error occurred on x.
x end
y start
Error occurred on y.
y end
Error occurred on .

When errtrace is not enabled:

x start
x end
y start
y end
Error occurred on .

这篇关于什么`在shell脚本设置-o errtrace`将执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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