入门书籍推荐

第二版

image-20250426123737992

image-20250426123819250

语法

image-20250426124034309

  • 变量替换

image-20250426124425629

替换的变量是由$后面的所有字母,数字和下划线组成。故遇到非字母,数字,下划线就会终止。

image-20250426124654063

  • 命令替换

image-20250426124726565

image-20250426125000249

  • 反斜杠替换

image-20250426125031427

  • 引用

image-20250426125543624

指令换行 \ 后面一定不要加空格,否则指令解析会出错

image-20250426125941234

三种匹配

  • exact匹配

image-20250426130204283

  • glob匹配

image-20250426130222398

image-20250426130233243

  • 正则表达式

image-20250426130358177

变量

  • 无需进行定义,直接使用set进行创建,unset移除,变量包含变量名和变量值
  • 使用$进行调用
  • 区分大小写
  • 变量名和变量值都可以由任意字符组成,但是建议使用字母开头,使用字母,数字和下划线来命名
  • 常用的变量类型有:string list array directionary,synopsys有collection
  • 变量存储分为字符串表达形式和内部表达式形式(整数,实数,名称,列表以及tcl脚本)

string

image-20250426132001379

  • match

image-20250426131906959

使用glob匹配方式进行匹配

例子:

image-20250426132200969

  • equal

image-20250426132216496

例子:

image-20250426132254154

  • compare

image-20250426133018125

例子:

image-20250426133035398

  • length

image-20250426133056931

例子:

image-20250426133126839

  • index

image-20250426133112931

例子:

image-20250426133241295

  • range
image-20250426133258714

例子:

image-20250426133517076

  • replace

image-20250426133648665

例子:

image-20250426133745421

  • map

image-20250426133803356

例子:

image-20250426133939638

  • trim裁剪

image-20250426135203177

例子:

image-20250426135216607

裁剪左边或者裁剪右边的,一般用于裁剪空格

image-20250426135251167

  • 大小写变换

image-20250426135648017

  • repeat

image-20250426135756665

image-20250426135747175

  • reverse

image-20250426135812648

list

  • 创建list
1
2
3
set list_a "fio0 fio1 fio2"
set list_a {fio0 fio1 fio2}
set list_a [list fio0 fio1 fio2]
  • list相关的命令

image-20250426141013272

  • llength
1
llength $list_a

image-20250426141101405

  • lindex

image-20250426141152297

  • lrange

与list index相似,只不过它需要两个索引, 返回first到last之间的字符。如果first小于0, 则按0来处理;last也是类似

image-20250426141347465

  • linsert

在list的指定位置插入新元素

image-20250426141550457

  • lreplace

把list中从first到last的元素换成零个或者多个元素

image-20250426143125337

删除最后一个元素应该是:lreplace $list_a end end

  • lset

将变量varName中的index位置的元素改成newValue

image-20250426143420806

  • concat

将不同的list合并成新的list

image-20250426143518893

image-20250426143529556

  • lappend

image-20250426143618683

把value作为列表元素添加到varName变量中,如果这个变量不存在,就创建它

image-20250426143645261

  • lrepeat

将value重复number次,得到新的list

image-20250426143716088

  • lassign

将list的元素依次赋给名为varName的变量

image-20250426143742593

  • join

image-20250426144110196

把joinString作为分隔符,把列表元素串接起来。joinString默认为空格。(可以将list转化成string)

image-20250426145259479

  • split

image-20250426145319237

把string在splitChars处分开(将string转成list)

image-20250426150029575

  • lsearch

image-20250426150129151

在list中搜索与pattern匹配的一个或多个元素。Option选项控制模式匹配方式(-exact,-glob, regexp),是返回元素值(-inline)还是索引,是搜索所用的匹配(-all)还是只搜索最先出现的匹配等 。默认使用-glob匹配,返回第一处匹配的索引,没有匹配元素时返回-1

image-20250426150615793

array

image-20250426150733744

表达式

image-20250426150853251

流控制

if

image-20250426151035982

image-20250426151235016

while

image-20250426151305459

参数test是一个表达式,body是一个脚本。如果表达式结果为非零,则运行脚本,直到表达式等于零才停止循环。此时while命令中断并返回一个空字符串

image-20250426151412227

foreach

image-20250426151608951

varname是一个变量;list是一个列表;body是循环体

image-20250426151700012

image-20250426152055925

switch

image-20250426152120028

第一个参数是可选参数options,表示匹配的方式。Switch支 持三种匹配方式:-exact方式(默认),-glob方式和-regexp 方式

image-20250426152611051

  • break/continue

image-20250426153227022

函数

函数的基本结构

image-20250426153336210

函数的三种写法

  • 写法一

image-20250426153605969

  • 带缺省值

image-20250426154320086

  • 可变参数

image-20250426154624833

synopsys的函数结构

image-20250426160348639

这种函数结构是带-help的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 1. 先定义命令组(如果不存在)
create_command_group {my_procs} -info "useful utilities, added by zzb"

# synopsys的函数结构
proc func_3 {args} {
parse_proc_arguments -args $args arguments
if {[info exists arguments(-type)]} {
if {[string equal $arguments(-type) add]} {
return [expr {$arguments(num0) + $arguments(num1)}]
} elseif {[string equal $arguments(-type) mult]} {
return [expr {$arguments(num0) * $arguments(num1)}]
}
} else {
return [expr {$arguments(num0) + $arguments(num1)}]
}
}

define_proc_attributes func_3 \
-info "logic function" \
-command_group my_procs \
-define_args \
{
{-type "add or mult, default add" "type" string optional}
{num0 "number 0" "num0" string required}
{num1 "number 1" "num1" string required}
}
  • 创建command group

image-20250426163648612

  • 查看所有的list_command

image-20250426163710021

  • 查看command group对应的函数

image-20250426163735177

文件操作

image-20250426164513985

  • glob文件操作

image-20250426165700229

  • file文件操作

image-20250426170411038

常用命令

  • exec:用于执行shell命令

image-20250426170654165

  • redirect:重定向

image-20250426170721452

  • regexp:正则匹配

image-20250426170744761

练习题

image-20250426134439531

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 任意长度排序,达到小或者小到大
proc num_sort {args} {
parse_proc_arguments -args $args arguments
echo "the length of the nums is [llength $arguments(nums)]"
set input_nums [split $arguments(nums) ","]
echo $input_nums
if {[info exists arguments(-type)]} {
if {[string equal arguments(-type) max2min]} {
set result [lsort -decreasing -real $input_nums]
} else {
set result [lsort -increasing -real $input_nums]
}
} else {
set result [lsort -decreasing -real $input_nums]
}
set w_file_handle [open "sort_result.txt" w]
foreach i $result {
puts $w_file_handle $i
}
close $w_file_handle
set ouptut_nums [join $result ","]
echo $ouptut_nums
}


define_proc_attributes num_sort \
-info "sort the nums, max to min or min to max" \
-command_group my_procs \
-define_args \
{
{-type "max2min min2max, default is max2min" "type" string optional}
{nums "sort numbers" "nums" string required}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 任意长度排序,达到小或者小到大
proc num_sort_removerepeat {args} {
parse_proc_arguments -args $args arguments
echo "the length of the nums is [llength $arguments(nums)]"
set input_nums {}
# 去除重复的数据
foreach number $arguments(nums) {
if {[lsearch -exact $input_nums $number] >= 0} {
set input_nums $input_nums
} else {
lappend input_nums $number
}
}
echo $input_nums
if {[info exists arguments(-type)]} {
if {[string equal arguments(-type) max2min]} {
set result [lsort -decreasing -real $input_nums]
} else {
set result [lsort -increasing -real $input_nums]
}
} else {
set result [lsort -decreasing -real $input_nums]
}
set w_file_handle [open "sort_result.txt" w]
foreach i $result {
puts $w_file_handle $i
}
close $w_file_handle
}


define_proc_attributes num_sort_removerepeat \
-info "sort the nums, max to min or min to max, the ouptut number just appear once" \
-command_group my_procs \
-define_args \
{
{-type "max2min min2max, default is max2min" "type" string optional}
{nums "sort numbers" "nums" list required}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
proc file_func {args} {
parse_proc_arguments -args $args arguments
set file_list [glob $arguments(file_dir)/*.txt]
set target_files {}
# 正则匹配,去除有数字的
foreach file_name $file_list {
if {![regexp {[0-9]} $file_name]} {
lappend target_files $file_name
}
}
echo $target_files
set result {}
foreach file_name $target_files {
lappend result $file_name
set one_number 0
set read_file_handle [open "$file_name" r]
while {![eof $read_file_handle]} {
gets $read_file_handle line
if {[regexp {1} $line]} {
incr one_number 1
}
}
close $read_file_handle
lappend result $one_number
}
echo $result
}

define_proc_attributes file_func \
-info "file function" \
-command_group my_procs \
-define_args \
{
{file_dir "the function dir" "file_dir" string required}
}