在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
作为行号。有没有办法从陷阱处理程序中获取失败函数的实际行号?
答案 0 :(得分:3)
这似乎是在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 report。