在函数内部,如何获取调用该函数的任何脚本的文件名?zsh:获取调用函数的脚本的文件名
通过提示扩展,%x
提供了定义函数的文件的名称。 %N
提供了函数名称。我如何获得调用函数的文件名?
这将是有益的汇报我的每一个启动脚本的运行时间,例如:
~/.zshenv:
function start_timer() {
start_time=$(date +%s.%N)
}
function stop_timer() {
stop_time=$(date +%s.%N)
elapsed_time=$((stop_time-start_time))
echo "$SCRIPTNAME took $elapsed_time seconds"
}
start_timer
# initialize env...
end_timer # "~/.zshenv took 0 seconds"
~/.zshrc:
start_timer
# initialize interactive shell...
end_timer # "~/.zshrc took 2 seconds"
~/.zlogin:
start_timer
# initialize login shell...
end_timer # "~/.zlogin took 2 seconds"
这是[非常相似的问题](http://stackoverflow.com/questions/9901210),但也没有真正的解决方案。 – Lucas