从陷阱返回处理程序中的函数访问返回code [英] access return code of a function from within the trap RETURN handler
本文介绍了从陷阱返回处理程序中的函数访问返回code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在bash中,如何能返回
处理器接入的电流返回code?
Within bash, how can a RETURN
handler access the current return code?
例如
#!/usr/bin/env bash
function A() {
function A_ret() {
# How to access the return code here?
echo "${FUNCNAME} ???"
}
trap A_ret RETURN
echo -n "${FUNCNAME} returning code $1 ... "
return $1
}
A 1
这版画
A returning code 1 ... A_ret ???
我想它来打印
A returning code 1 ... A_ret 1
哪有 A_ret
访问 A
收益code?
要这个计算器的问题类似的获取shell脚本的退出code在陷阱EXIT 。
推荐答案
它看起来像返回
陷阱返回之前执行
语句实际上是设置 $?
的新值。考虑下面这个例子,设置 $?
在返回之前
语句。
It looks like the RETURN
trap executes before the return
statement actually sets the new value of $?
. Consider this example that sets $?
just before the return
statement.
a () {
a_ret () {
echo "${FUNCNAME} $?"
}
trap a_ret RETURN
printf "${FUNCNAME} returning code $1 ... "
(exit 54)
return $1
}
a 1
在庆典
3.2和4.3,我得到作为输出
In bash
3.2 and 4.3, I get as output
a returning code 1 ... a_ret 54
我会说这是报告中的错误。作为一种变通方法,您可以随时使用子shell 退出
与您要返回值:
a () {
...
(exit $1)
return $1
}
这篇关于从陷阱返回处理程序中的函数访问返回code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文