从源文件中查找函数调用的行号 [英] Find line number of function call from sourcing file
本文介绍了从源文件中查找函数调用的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找到一种方法来找出函数是从哪个文件和行号调用的.该函数位于由我的脚本提供的库文件中.
I'm trying to find a way to find out what file and line number a function was called from. The function is in a library file which is being sourced by my script.
文件 1:
$source file2
$warn_me "Error: You didn't do something"
文件 2:
$function warn_me() {
$ message=????
$ echo ${message}
$}
期望的输出: $: file1:Line 2:错误:你没有做任何事情
函数调用已经在许多文件中多次出现,所以我试图找到一种方法来做到这一点而不改变它.
The function call already occurs many times in many files so I'm trying to find a way to do this without changing that.
以前在使用它的每个文件中都定义了warn_me 函数,并像这样处理:
Previously the warn_me function was defined in every file that used it and this was taken care of like so:
$local message="$BASH_SOURCE:(""${BASH_LINENO}): ""$*"
推荐答案
您正在寻找 caller
好像.
You are looking for caller
it seems.
$ cat h.sh
#! /bin/bash
function warn_me() {
echo "$@"
caller
}
$ cat g.sh
#!/bin/bash
source h.sh
warn_me "Error: You didn't do something"
$ . g.sh
Error: You didn't do something
3 g.sh
这篇关于从源文件中查找函数调用的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文