Don*_*ows 17
在评估脚本时(但不一定在调用其过程时),当前脚本名称(严格地说,传递给它的任何内容source
或C API等价物,Tcl_EvalFile()
并且相关)是结果info script
; 将其标准化为绝对路径名是非常明智的,这样任何调用cd
都不会改变解释.
需要信息的脚本往往会在自己内部放置这样的东西:
# This is a *good* use of [variable]…
variable myLocation [file normalize [info script]]
Run Code Online (Sandbox Code Playgroud)
然后,他们可以轻松地检索值(或从中派生的东西):
proc getResourceDirectory {} {
variable myLocation
return [file dirname $myLocation]
}
Run Code Online (Sandbox Code Playgroud)
其他常见的位置是:
$::argv0
这是"主要"脚本(如果它等于,你正在评估主要脚本info script
)[info nameofexecutable]
这是Tcl解释程序本身(通常argv[0]
是C级)[info library]
这是Tcl自己的库脚本所在的位置$::tcl_pkgPath
这是安装包的目录的Tcl列表$::auto_path
这是一个搜索脚本的目录的Tcl列表(包括包!包路径用于初始化它.)我发现这样做的最好方法:
set script_path [ file dirname [ file normalize [ info script ] ] ]
puts $script_path
Run Code Online (Sandbox Code Playgroud)