系统学习TCL脚本入门教程_第1页
系统学习TCL脚本入门教程_第2页
系统学习TCL脚本入门教程_第3页
系统学习TCL脚本入门教程_第4页
系统学习TCL脚本入门教程_第5页
已阅读5页,还剩57页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、系统学习TCL脚本入门教程 版本:1. 0目 录1TCL语法41.1简介41.2运行环境41.3本文约定41.4参考资料42引言52.1第1课:简单文本输出52.2第2课:给变量赋值52.3第3课:命令的赋值与置换一62.4第4课:命令的赋值与置换二72.5第5课:命令的赋值与置换三72.6第6课:算数运算82.7第7课:文本比较SWITCH应用92.8第8课:数值比较IF应用102.9第9课:WHILE 循环112.10第10课:FOR循环和incr112.11第11课:过程PROC122.12第12课:过程PROC的参数定义132.13第13课:变量的作用域132.14第14课:LIST结

2、构142.15第15课:LIST项的增删改152.16第16课:更多LIST相关162.17第17课:字符串函数172.18第18课:更多字符串函数172.19第19课:修改字符串函数192.20第20课:正则表达式212.21第21课:更多正则表达式222.22第22课:数组242.23第23课:更多数组相关252.24第24课:文件存取282.25第25课:文件信息302.26第26课:TCL中的子进程调用open & exec332.27第27课:命令或者变量是否存在info342.28第28课:解释器状态info352.29第29课:过程信息info362.30第30课:模块化

3、source372.31第31课:建库unknown & info library382.32第32课:创建命令eval402.33第33课:在eval中应用format & list402.34第34课:不使用eval替换format & subst422.35第35课:改变工作目录 cd & pwd432.36第36课:调试和错误errorinfo & errorCode & catch432.37第37课:调试trace452.38第38课:命令行参数和环境串462.39第39课:time & unset472.40第40课:soc

4、ket & fileevent & vwait492.41第41课:日期时间clock512.42第42课:i/o通道fblocked & fconfig532.43第43课:子解释器552.44第44课:数据库操作573跋601 TCL语法1.1 简介作为脚本语言,tcl语法简单而功能强大。它诞生于80年代初,进入中国的时间也许多年了。不过关于tcl的资料多而杂,一时心血来潮,想到写一篇文章,以例子为中心,系统讲解tcl语法,让技术人员花最少的时间对tcl有个全面而系统的了解,工作上使用时可以速查或参考代码。于是有了本文。1.2 运行环境多数代码运行在 ActiveT

5、cl 安装以后的windows环境中,只有两个例程运行在unix的环境下。1.3 本文约定本文为了便于速查和速学,所以每课分成讲解和例子两部分,主要是围绕着例子进行讲解。文字不多。1.4 参考资料本文主要参考了TclTutor 2.0 beta4。2 引言2.1 第1课:简单文本输出讲解:1注释符号是 或者 ;# ,在命令后注释用 ;# ,在行开头两者均可;2puts :输出文本,多个单词如被空格或TAB分隔需要使用“”或 括起来;3多个命令写在一行使用 ; 间隔。例子:002_puts.tcl# ok ;# 正确;# ok ;# 正确; # ok ;# 正确, 分号和井号之间可以有空格put

6、s Hello ;# 正确puts Hello,World ;# 正确,多个单词之间不是被空格或者TAB分隔开puts Hello World ;# 这行命令运行出错,被空格分隔puts "Hello, World - In quotes" ;# 注释puts Hello, World - In Braces# 这行命令运行出错,必须使用 ;# 作为注释符号puts "This is line 1" puts "this is line 2" ;# 正确,用分号分隔两个命令puts "Hello, World; - With

7、 a semicolon inside the quotes" ;#正确,分号在双引号内,作为字符串一部分2.2 第2课:给变量赋值讲解:1 set:给变量赋值,格式为 set var value例子:003_var.tcl;# 给变量X赋一个字符串set X "This is a string"# 给变量Y赋一个数字set Y 1.24;# 显示X和Y的内容puts $Xputs $Y;# 打印一个分隔串puts "."# 打印在一行中,推荐使用双引号set label "The value in Y is: "puts

8、"$label $Y"puts $label$Y2.3 第3课:命令的赋值与置换一讲解:1 TCL中命令的赋值分为置换和赋值两个步骤2 续行符为 3 转义符同为 4 特殊字符列表:序号字符输出十六进制1a响铃x072b回车x083f清屏x0c4n换行x0a5r回车x0d6t制表符x097v垂直制表符(Vertical Tab)x0b8ddd八进制值d=0-79xhh十六进制值h=0-9,A-F,a-f例子:004_eval.tcl;# Show how a affects the $set Z "Albany"set Z_LABEL "The

9、Capitol of New York is: "puts "$Z_LABEL $Z"#显示Albanyputs "$Z_LABEL $Z" ;#显示$Z,被 转义;# The next line needs a backslash to escape the '$'puts "nBen Franklin is on the $100.00 bill" ;# n换行; $100前的 必须有,否则会将100作为一个变量,提示出错set a 100.00puts "Washington is not o

10、n the $a bill" ;# This is not what you wantputs "Lincoln is not on the $a bill" ;# 显示$100,说明是后结合的,先置换了$a,此处严格的写应该写为 $aputs "Hamilton is not on the $a bill" ;# 显示$aputs "Ben Franklin is on the $a bill" ;# 显示$100,说明是后结合的,先置换了$aputs "n. examples of escape strings

11、"puts "TabtTabtTab"puts "This string prints out non two lines" ;# 行中 没有打印出来,如果要打印出来,需要写成 puts "This string comes outon a single line" ;# 当一行太长,不便于阅读,使用 做续行符2.4 第4课:命令的赋值与置换二讲解:1最外层是 则不会进行置换操作,但其中的续行符仍然有效例子:005_escape.tclset Z "Albany"set Z_LABEL "The

12、 Capitol of New York is: "puts "n. examples of differences between " and " ;#and前的双引号前必须有 进行转义,否则这个双引号回和前面的双引号结合, 导致成了 “xxx” and “ 的结构,会提示出错puts "$Z_LABEL $Z" ;# 显示The Capitol of New York is: Albanyputs $Z_LABEL $Z ;# 显示 $Z_LABEL $Z,没有进行置换,中不会置换puts "n. examples of

13、 differences in nesting and " "puts "$Z_LABEL $Z" ;# 最外层是双引号,所以进行了置换puts Who said, "What this country needs is a good $Z cigar!"? ;#最外层是花括号,所以没有进行置换puts "n. examples of escape strings"puts There are no substitutions done within braces n r x0a f v ;#puts But, t

14、he escaped newline at the end of astring is still evaluated as a space ;#续行符仍然生效2.5 第5课:命令的赋值与置换三讲解:1 可以传递其中的命令结果,注意不能被 包含;2 双引号包含的 中的命令可以正常执行,命令结果也可以传出;3 包含的 中的命令不会执行,更不会有命令结果传出来。例子:006_escape.tclset x "abc"puts "A simple substitution: $xn" ;#显示abcset y set x "def" ;#先

15、执行中的命令,将”def”赋值给x,然后将该命令的结果赋值给yputs "Remember that set returns the new value of the variable: X: $x Y: $yn" ;#显示x和y都是defset z set x "This is a string within quotes within braces" ;#由于在中,所以并没有执行对x的赋值,只是将赋值给zputs "Note the curly braces: $zn"set a "set x This is a str

16、ing within braces within quotes" ;#执行了对x的赋值操作,并将值传出来赋给了aputs "See how the set is executed: $a"puts "$x is: $xn"set b "set y This is a string within braces within quotes"puts "Note the escapes the bracket:n $b is: $b"puts "$y is: $y"2.6 第6课:算数运算讲

17、解:1 操作符序号操作符解释1- + !- : 负号 + : 正号 : 位操作非 ! : 逻辑非2* / %* : 乘 / : 除 % : 取模3+ -+ : 加 - : 减4<< >><< : 循环左移 >> : 循环右移5&& : 按位与6 : 按位异或7| : 按位或8&&&& : 逻辑与9| : 逻辑或10x?y:zif-then-else2 数学函数序号函数序号函数1acos11log102cos12tan3hypot13atan24sinh14floor5asin15pow6cosh16

18、tanh7log17ceil8sqrt18fmod9atan19sin10exp例子:007_math.tclset X 100;set Y 256 ;# 行末是否有分号都可以set Z expr "$Y + $X" ;# 变量是否被双引号包含都可以,不过建议使用双引号set Z expr $Y + $Xset Z_LABEL "$Y plus $X is "puts "$Z_LABEL $Z"puts "The square root of $Y is expr sqrt($Y)n"puts "Beca

19、use of the precedence rules "5 + -3 * 4" is: expr -3 * 4 + 5"puts "Because of the parentheses "(5 + -3) * 4" is: expr (5 + -3) * 4"puts "n. more examples of differences between " and "puts $Z_LABEL expr $Y + $X ;#外层是花括号不会进行置换puts "$Z_LABEL expr

20、$Y + $X" ;# 外层是双引号会进行置换puts "The command to add two numbers is: expr $a + $b"2.7 第7课:文本比较SWITCH应用讲解:1 switch的分支中的命令使用花括号包含,但是并不会影响花括号中的命令执行,切记,这是switch的格式;2 如果不想分支条件进行置换,需要在外加上花括号,不会影响分支中的命令执行。例子:008_switch.tcl;# Set the variables we'll be comparingset x "ONE"set y 1;set

21、 z "ONE"# This is legalswitch $x "ONE" "puts ONE=1" "TWO" "puts TWO=2" "default" "puts NO_MATCH" ;#这种写法合法,但是阅读不便switch $x "ONE" "puts ONE=1" "TWO" "puts TWO=2" "default" "puts

22、 NO_MATCH" ;#这种写法好看一些,推荐;#下面这种写法$z被置换,走入$z的条件分支,表面上看条件分支中的命令在花括号内,这只是switch的一种格式,所以其中的命令仍然被执行了。switch $x "$z"set y1 expr $y+1; puts "MATCH $z. $y + $z is $y1" "ONE"set y1 expr $y+1; puts "MATCH ONE. $y + one is $y1" "TWO"set y1 expr $y+2; puts &

23、quot;MATCH TWO. $y + two is $y1" "THREE"set y1 expr $y+3; puts "MATCH THREE. $y + three is $y1" "default"puts "$x does not match any of these choices"# This form of the command disables variable substitution in the pattern;#下面为了不置换$z,在外层加上了花括号,于是走入了ONE分支,

24、而分支中的命令仍然被执行了switch $x "$z"set y1 expr $y+1; puts "MATCH $z. $y + $z is $y1" "ONE"set y1 expr $y+1; puts "MATCH ONE. $y + one is $y1" "TWO"set y1 expr $y+2; puts "MATCH TWO. $y + two is $y1" "THREE"set y1 expr $y+3; puts "MAT

25、CH THREE. $y + three is $y1" "default"puts "$x is NOT A MATCH" 2.8 第8课:数值比较IF应用讲解:1 条件式结果FALSETRUE数值0非零yes / nonoyestrue / falsefalsetrue2置换变量的方法,set y x ; puts $y ,因为是后结合并且是一次置换,所以打出来的是 $x ,不是$x的值;但是在if的条件式中进行了二次置换, $y 被置换成了 $x 的值3注意:新行中需要写为 else ,不能将 写到前一行的末尾,也不能省略 后面的那个空格

26、,后面的 也需要写在当行,并且前面需要一个空格。例子:009_if.tclset x 1;if $x = 2 puts "$x is 2" else puts "$x is not 2" ;#判断是否相等使用 =if $x != 1 ;#判断是否不等使用 != puts "$x is != 1" else puts "$x is 1" if $x=1 puts "GOT 1"set y x;if "$y != 1" ;#在if条件式中$y进行了二次置换 puts "

27、$y is != 1" ;#在puts命令中,只进行了一次置换 else puts "$y is 1" 2.9 第9课:WHILE 循环x讲解:1while后面的条件表达式是放在花括号中的;放在双引号中会只执行一次置换例子:010_while.tclset x 1;while $x < 5 puts "x is $x" set x expr $x + 1puts "exited first loop with X equal to $xn"set x 0;while "$x < 5" ;#只执

28、行一次置换1<5,于是该条件永远为真set x expr $x + 1if $x >6 break; ;#如果去掉这句就成了死循环 if "$x > 3" continue; ;#这句使4 打不出来puts "x is $x" puts "exited second loop with X equal to $xn"2.10 第10课:FOR循环和incr讲解:1incr x 和 set x expr $x + 1 达到一样的效果,向上加一x例子:011_for.tclfor puts "Start&quo

29、t; set i 0 $i < 2 incr i; puts "I after incr: $i" ;#第一部分只执行一次,后面两部分每次循环都会执行 puts "I inside first loop: $i" for puts "Start" set i 3 $i < 2 incr i; puts "I after incr: $i" ;#不会执行循环体中的命令 puts "I inside second loop: $i" puts "Start" set

30、i 0;while $i < 2 puts "I inside first loop: $i" incr i; puts "I after incr: $i" 2.11 第11课:过程PROC讲解:1 格式:proc name args body2 调用方法中参数可以用花括号或者双引号包含,也可以不包含3 在puts等命令中需要置换的话,需要使用方括号例子:012_proc.tclproc sum arg1 arg2 set x expr $arg1+$arg2; return $x ;#过程返回值 puts " The sum of 2

31、 + 3 is: sum 2 3nn" ;#调用过程#puts " The sum of 2 + 3 is: sum 2 3nn" ;#出错,提示找不到第二个参数,置换过程中第一个参数是2 3,所以找不到第二个参数puts " The sum of 2 + 3 is: sum(2 3)nn" ;#输出sum(2 3),因为没有方括号,根本没有进行置换puts " The sum of 2 + 3 is: sum2 3nn" ;#输出sum2 3,因为没有方括号,根本没有进行置换sum 2 3 ;#正确sum 2 3 ;#正确

32、sum "2" "3" ;#正确proc for a b c puts "The for command has been replaced by a puts"puts "The arguments were: $an$bn$cn"for set i 1 $i < 10 incr i2.12 第12课:过程PROC的参数定义讲解:1 过程的参数赋缺省值:proc name arg1 arg2 value2 过程的不确定个数的参数定义:proc name arg1 args例子:013_proc.tclpro

33、c example first second "" args ;#参数定义:赋缺省值和不确定个数参数定义 if $second = "" puts "There is only one argument and it is: $first" return 1; else if $args = "" puts "There are two arguments - $first and $second" return 2; else puts "There are many argumen

34、ts - $first and $second and $args" return "many" set count1 example ONEset count2 example ONE TWOset count3 example ONE TWO THREE set count4 example ONE TWO THREE FOURputs "The example was called with $count1, $count2, $count3, and $count4 Arguments"2.13 第13课:变量的作用域x讲解:1 全局变

35、量定义:global var12 局部变量:upvar x y 等同于upvar 1 x y,作用有两个:一是将上一层的x的值赋给y;二是将上一层的x的地址赋给y,于是修改y等于修改x。1代表作用范围,也可为2,3等,不能为0例子:014_varscope.tclproc SetPositive variable value ;#此处variable只是一个参数名,可以修改为其他的来代替变量 upvar $variable myvar ;#此处也可写为upvar 1 $variable myvar if $value < 0 set myvar expr -$value; else se

36、t myvar $value; return $myvar; SetPositive x 5;SetPositive y -5;puts "X : $x Y: $yn"proc two y upvar 1 $y z;#此处绑定了two中的z和one中的y upvar 2 x a;# 此处绑定了主程序中的x和two中的a puts "two: Z: $z A: $a" set z 1;# Set z, the passed variable to 1; set a 2;# Set x, two layers up to 2; ;# A first leve

37、l proc - This will be called by the global space c one y upvar $y z;# This ties the calling value to variable z puts "one: Z: $z"# Output that value, to check it is 5 two z;# call proc two, which will change the value one y;# Call one, and output X and Y after the call.puts "n

38、X: $x Y: $y"2.14 第14课:LIST结构讲解:1 list结构下标是从零开始的,引用方式是lindex list 位置-12 字符串可以使用 :split 字符串 分隔符 拆分得到一个list,缺省分隔符是空格3 list可以直接定义:set z list a b4 foreach x $list :用以列出list中的所有项5 llength $list :用以列出list中的项数例子:015_list.tclset x "a b c"puts "Item 2 of the list $x is: lindex $x 2n"

39、;#引用list的第三项(从0开始)的值c使用函数lindex,并使用方括号set y split 7/4/1776 "/" ;#等同于set y split “7/4/1776” "/"puts "We celebrate on the lindex $y 1'th day of the lindex $y 0'th monthn"set z list puts "arg 2 is $y" ;#此处的list z是两个项:puts和” arg 2 is $y”puts "A comman

40、d resembles: $zn" ;#这里的输出是:A command resembles: puts arg 2 is 7 4 1776,注意:1。当有多个子项时自动使用了花括号来明确是一项;2。由于是双引号保护,对$y进行了置换set i 0;foreach j $x ;#注意这里是foreach j $x 而不是 foreach $j $x puts "$j is item number $i in list x" incr i; 2.15 第15课:LIST项的增删改讲解:1 在中执行的命令不会改变其中变量的值,在外面单独执行会改变其值;2 list函数

41、列表:序号函数解释1concat ?arg1 arg2 .argn合并list2lappend listname ?arg1 arg2 .argn在list后增加项3linsert listname index arg1 ?arg2 .argn在list中插入项4lreplace listname first last ?arg1 arg2 .argn替代list中的项例子:016_list.tclset b list a b c d e f g h ;#为4项:a; b; c d e;f g hputs "Treated as a list: $bn"set b spli

42、t "a b c d e f g h"puts "Transformed by split: $bn" ;#输出为:a b c d e f g hset a concat a b c d e f g hputs "Concated: $an" ;#concat去掉了第一层花括号,输出:a b c d e f g hlappend a ij K lm;#在a后面增加了一项ij K lm,注意此处a的值改变了lappend a ij K lm;#在a后面增加了三项 ij K lm,注意此处a的值改变了puts "After la

43、ppending: $an"set b linsert $a 3 "1 2 3" ;# 在a的第三项(0开始数)插入一项”1 2 3” ,注意此处a的值并没有被改变set b linsert $a 4 1 2 3 ;# 在a的第三项(0开始数)插入一项1 2 3,注意此处a的值并没有被改变set b linsert $a 1 1 2 3 ;# 在a的第三项(0开始数)插入三项 1 2 3,注意此处a的值并没有被改变puts "After linsert at position 3: $bn"# "AA" and "

44、BB" are two list elements.set b lreplace $b 3 5 "AA" "BB" ;#注意是第三到五项(0开始数)被替换puts "After lreplacing 3 positions with 2 values at position 3: $bn"2.16 第16课:更多LIST相关讲解:1 list相关的函数列表:序号函数解释1lsearch list pattern按照某种模式查找list中的项,返回满足条件的第一项的出现位置2lsort list对list排序3lrange l

45、ist first last从list中取出一个范围的项lsort -mode list排列列表。-mode : -ascii-dictionary 与acsii类似,只是不区分大小写-integer 转化为整数再比较-real 转化为浮点数再比较-command command 执行command来做比较2 通配符列表序号通配符解释1*代表任意字符2?代表一个字符3X转义符4.代表一个集合例子:017_list.tclset list1 list a b cset bpos lsearch $list1 bputs "b position : $bpos" ;#返回位置值

46、1set list list Washington 1789 Adams 1797 Jefferson 1801 Madison 1809 Monroe 1817 Adams 1825 set x lsearch $list Washington* ;#返回0set y lsearch $list Madison* ;#返回3set y lsearch $list M* ;#返回满足条件的第一项的位置3#set x lsearch $list 17* ;#返回-1,没有找到满足条件的项#set y lsearch $list 180? ;#返回-1,没有找到满足条件的项incr x; incr

47、 y -1; ;# Set range to be not-inclusiveset subsetlist lrange $list $x $yputs "The following presidents served between Washington and Madison"foreach item $subsetlist puts "Starting in lindex $item 1: President lindex $item 0 " set x lsearch $list Madison*set srtlist lsort $list;s

48、et y lsearch $srtlist Madison*;puts "n$x Presidents came before Madison chronologically"puts "$y Presidents came before Madison alphabetically"2.17 第17课:字符串函数讲解:1字符串函数列表序列函数解释1string length返回字符串的长度2string index返回字符串相应位置的字符3string range返回字符串中一个范围内的字符子串例子:018_string.tclset string &

49、quot;this is my test string"puts "There are string length $string characters in "$string""puts "string index $string 1 is the second character in "$string"" ;#返回hputs ""string range $string 5 10" are characters between the 5'th and 10&#

50、39;th" ;#返回”is my ”2.18 第18课:更多字符串函数讲解:1字符串函数列表序号函数解释1string compare string1 string2字符串比较返回:-1 :string1比string2小0 :string1和string2相等1 :string1比string2大2string first string1 string2返回string1在string2中第一次出现的位置;如果string2不在string1中,返回-13string last string1 string2返回string1在string2中最后一次出现的位置;如果string

51、2不在string1中,返回-14string wordstart string1 index返回string1中index处的单词的开始位置5string wordend string1 index返回string1中index处的单词的结束位置6string match pattern string1返回string1中是否满足匹配模式pattern匹配模式的通配符:* :任意字符? :单个字符X :转义符. :字符区间,例如:a-z例子:019_stringcmp.tclset fullpath "/usr/home/clif/TCL_STUFF/TclTutor/Lsn.17

52、"set relativepath "CVS/Entries"set directorypath "/usr/bin/"set paths list $fullpath $relativepath $directorypathforeach path $paths set first string first "/" $path; set last string last "/" $path;#根据开头是否是 来判断是相对路径还是绝对路径 if $first != 0 puts "$path i

53、s a relative path" else puts "$path is an absolute path" ;# If "/" is not the last character in $path, report the last word. ;# else, remove the last "/", and find the next to last "/", and ;# report the last word. incr last; if $last != string length $pa

54、th set name string range $path $last end; puts "The file referenced in $path is $name" else incr last -2; set tmp string range $path 0 $last; set last string last "/" $tmp; incr last; set name string range $tmp $last end puts "The final directory in $path is $name" ;# 如果是包含CVS,判断名字开头的大小写 if string match "*CVS*" $path;#注意和lsearch格式的区分,lsearch list pattern,匹配模式是在后面 puts "$path is part of the source code control tree" ;#判断一个名字开头是大写还是小写字母 set comparison string compare $name "a" if $comparison >= 0 puts "$name starts

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论