Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
zsh:获取调用函数的脚本的文件名_Zsh_Zshrc - Fatal编程技术网

zsh:获取调用函数的脚本的文件名

zsh:获取调用函数的脚本的文件名,zsh,zshrc,Zsh,Zshrc,在函数内部,如何获取调用该函数的脚本的文件名 通过提示展开,%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)) ech

在函数内部,如何获取调用该函数的脚本的文件名

通过提示展开,
%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"
start_timer
# initialize interactive shell...
end_timer # "~/.zshrc took 2 seconds"
start_timer
# initialize login shell...
end_timer # "~/.zlogin took 2 seconds"
~/.zshrc:

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"
start_timer
# initialize interactive shell...
end_timer # "~/.zshrc took 2 seconds"
start_timer
# initialize login shell...
end_timer # "~/.zlogin took 2 seconds"
~/.zlogin:

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"
start_timer
# initialize interactive shell...
end_timer # "~/.zshrc took 2 seconds"
start_timer
# initialize login shell...
end_timer # "~/.zlogin took 2 seconds"

可以将名称作为参数传递

~/.zshenv:

function start_timer() {
  echo "File: `basename $@`"             # <----
  start_time=$(date +%s.%N)
}
basename仅剥离最后一个斜杠之前的所有内容,以防您只想显示文件名

我的测试:

 File: aliases.zsh
 took 0.0018649101257324219 seconds
并且-如果您自己调用脚本,还可以使用
time
作为前缀命令来打印脚本的执行时间

$ time touch x
touch x  0.00s user 0.00s system 7% cpu 0.019 total

可以将名称作为参数传递

~/.zshenv:

function start_timer() {
  echo "File: `basename $@`"             # <----
  start_time=$(date +%s.%N)
}
basename仅剥离最后一个斜杠之前的所有内容,以防您只想显示文件名

我的测试:

 File: aliases.zsh
 took 0.0018649101257324219 seconds
并且-如果您自己调用脚本,还可以使用
time
作为前缀命令来打印脚本的执行时间

$ time touch x
touch x  0.00s user 0.00s system 7% cpu 0.019 total

至少在zsh 5.7.1中,
zsh_ARGZERO
变量将报告当前脚本的名称,即使调用它的函数不同:

$ cat foo
function a_function_in_foo {
    echo "ZSH_ARGZERO=$ZSH_ARGZERO"
}
$ cat bar
source foo
a_function_in_foo
$ zsh bar
ZSH_ARGZERO=bar
该文件记录在as

ZSH_ARGZERO

如果调用zsh来运行脚本,则这是脚本的名称。否则,它是用于调用当前shell的名称。这与设置POSIX_ARGZERO选项时$0的值相同,但始终可用


至少在zsh 5.7.1中,
zsh_ARGZERO
变量将报告当前脚本的名称,即使调用它的函数不同:

$ cat foo
function a_function_in_foo {
    echo "ZSH_ARGZERO=$ZSH_ARGZERO"
}
$ cat bar
source foo
a_function_in_foo
$ zsh bar
ZSH_ARGZERO=bar
该文件记录在as

ZSH_ARGZERO

如果调用zsh来运行脚本,则这是脚本的名称。否则,它是用于调用当前shell的名称。这与设置POSIX_ARGZERO选项时$0的值相同,但始终可用


当由zsh初始化时,
$0
在每个启动文件中都是
zsh
。如果我直接将脚本名传递给函数,比如
end_time${(%):-%x}
,这会起作用,但是您可以看到为什么我不希望在每次调用时使用它:)当zsh初始化时,
$0
在每个启动文件中都是
zsh
。如果我直接将脚本名传递给函数,它会起作用,比如
end\u time${(%):-%x}
,但是您可以看到为什么我不希望在每次调用时使用它:)这是一个但也不是真正的解决方案。这是一个但也不是真正的解决方案。在使用Debian的zsh 5.8上:
$zsh_ARGZERO
函数中,当从
终端调用脚本时,会回显
zsh
,并且
~/.vscode/extensions/rogalmic.zsh-debug-0.1.3/zshdb_dir/zshdb
当使用
zsh debug
扩展从
vscode
运行时。在使用Debian的zsh 5.8上:当从
终端调用脚本时,在函数echos
zsh
中使用
$zsh ARGZERO
,并且
~/.vscode/extensions/rogalmic.zsh-debug-0.1.3/zshdb_dir/zshdb
vscode
使用
zsh debug
扩展运行时。