调用者0在陷阱处理程序中给出错误的行号

当在Bash陷阱函数中使用调用者内建函数时, caller 0的结果给出了错误的行号,总是给出1 。 例如:

#!/bin/bash
function foo {
    exit 1
}
function bar {
    foo
}
function err {
    (( i = 0 ))
    while caller $i; do
        (( ++i ))
    done
}
trap err EXIT
bar

给出以下输出:

1 foo ./test.sh
6 bar ./test.sh
15 main ./test.sh

虽然i > 0的输出是正确的,但当在陷阱处理程序中使用caller 0 ,它似乎总是给出1作为行号。 有什么办法从陷阱处理程序获取失败函数的真实行号?


这似乎是在3.2.57(1)发布后的某个时候发布的错误:

$ bash -version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)
Copyright (C) 2007 Free Software Foundation, Inc.

$ bash ./test.sh
3 foo ./test.sh
6 bar ./test.sh
15 main ./test.sh

$ /usr/local/bin/bash --version
GNU bash, version 4.3.30(1)-release (x86_64-apple-darwin14.1.0)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$ /usr/local/bin/bash ./test.sh
1 foo ./test.sh
6 bar ./test.sh
15 main ./test.sh

似乎已经有一个bash项目的bug报告。

链接地址: http://www.djcxy.com/p/23755.html

上一篇: caller 0 gives wrong line number in trap handler

下一篇: Best practice to handle third party SSL certificate in Java