tcl语法网站:http://www.tcl.tk/man/tcl8.5/TclCmd/contents.htm
1)拷贝文件
set PRJ_HDL_DIR "../prj/hdl"
foreach filename [glob "../tsbs/smii_mtip/rtl/include/{*.verilog,*.v}"] {
file copy $filename $PRJ_HDL_DIR
}
2)如何获取当前脚本文件的名称?
由于有时需要获得当前运行脚本的名称,可以参考 如下
http://blog.csdn.net/gllg1314/article/details/7695746
http://wiki.tcl.tk/1384
http://stackoverflow.com/questions/23285360/how-to-get-path-of-current-script
While a script is being evaluated (but not necessarily while its procedures are being called) the current script name (strictly, whatever was passed to source or the C API equivalent, Tcl_EvalFile() and related) is the result of info script; it's highly advisable to normalise that to an absolute pathname so that any calls to cd don't change the interpretation.
Scripts that need the information tend to put something like this inside themselves:
# This is a *good* use of [variable]…
variable myLocation [file normalize [info script]]
They can then retrieve the value (or things derived from it) easily:
proc getResourceDirectory {} {
variable myLocation
return [file dirname $myLocation]
依据参考得到的代码:
set print echo
$print "----------------------------------------"
$print " rs - Re-Source startup file "
$print " comp_afu_textswap - Compile memcopy example "
$print " sim - Start vsim"
$print "----------------------------------------"
variable My_File [file normalize [info script]]
# the above line can't be put in the rs{} procedure.
proc rs {} {
variable My_File
set filename [file tail $My_File]
echo "current file is $filename"
source $filename
}
需要指出的是
a.variable My_File [file normalize [info script]] 这句话不能放在rs procedure中,否则得到空字符
b.rs函数中的variable My_File就是全局变量的My_File的值
===========================
1)
#set PRJ_HDL_DIR {$PRJ_ROOT_DIR}/prj/hdl 则[ Line 3: extra characters after close-brace ]
#set PRJ_HDL_DIR "{$PRJ_ROOT_DIR}/prj/hdl" 则{}会被当成字符,当成了 {..}/prj/hdl"
#set PRJ_HDL_DIR ${PRJ_ROOT_DIR}/prj/hdl correct!!!
#set PRJ_HDL_DIR "${PRJ_ROOT_DIR}/prj/hdl" correct!!
2)
{}里面不能有注释?
foreach pathname {1 2 3} ${
# foreach filename [glob "../tsbs/smii_mtip/rtl/include/{*.verilog,*.v}"] {
puts "pathname = $pathname"
# foreach filename [glob "${pathname}{*.verilog,*.v}"] {
# file copy $filename $PRJ_HDL_DIR
# }
}
Error: Failure when executing Tcl script. [ Line 10: can't read "
# foreach filename [glob "../tsbs/smii_mtip/rtl/include/{*.verilog,*.v": no such variable ]
Error: The Execute Script command failed.
3)
pathname = ${PRJ_ROOT_DIR}/tsbs/smii_mtip/rtl/include/
pathname = $PRJ_ROOT_DIR/tsbs/sgmii_mtip/rtl/include/
The Execute Script command succeeded.
{} 中间不能替换,但是foreach需要列表,其实是不需要,如下就能正确工作
set INCLUDE_PATH_ARRAY "${PRJ_ROOT_DIR}/tsbs/smii_mtip/rtl/include/ $PRJ_ROOT_DIR/tsbs/sgmii_mtip/rtl/include/"
foreach pathname $INCLUDE_PATH_ARRAY {
puts "pathname = $pathname"
}
本文来自博客园,作者:{e_shannon},转载请注明原文链接:https://www.cnblogs.com/e-shannon/p/5820993.html