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

下载本文档

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

文档简介

1、系统学习 TCL 脚本入门教程版本: 1. 0作者:次1 TCL语法41.1 简介41.2 运行环境41.3 本文约定41.4 参考资料42 引言52.1第1课:简单文本输出 522第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的参数定

2、义132.13 第13课:变量的作用域 132.14 第 14 课:LIST 结构142.15 第15课:LIST项的增删改 152.16 第16课:更多 LIST相关 162.17 第17课:字符串函数 172.18 第18课:更多字符串函数 172.19 第19课:修改字符串函数 202.20 第20课:正则表达式 212.21 第21课:更多正则表达式 222.22 第22课:数组242.23 第23课:更多数组相关 252.24 第24课:文件存取 282.25 第25课:文件信息 302.26 第26课:TCL中的子进程调用OPEN& exec 332.27 第27课:命令或者变量是

3、否存在INFO 342.28 第28课:解释器状态INFO352.29 第29课:过程信息info362.30 第 30 课:模块化SOURCE372.31 第 31 课:建库UNKNOWN & INFO LIBRARY 382.32 第32课:创建命令 EVAL 402.33 第 33课:在 eval 中应用 format& list402.34 第 34 课:不使用 EVAL 替换FORMAT& SUBST 422.35 第35课:改变工作目录 CD&PWD432.36 第 36 课:调试和错误 errorinfo & errorcode & catch 442.37 第37 课:调试TR

4、ACE 452.38 第38课:命令行参数和环境串462.39 第39 课:time & unset 472.40 第40 课:SOCKET & FILEEVENT&VWAIT 492.41 第41课:日期时间CLOCK 512.42 第42 课:I/O 通道FBLOCKED&FCONFIG 532.43 第43课:子解释器 562.44 第44课:数据库操作 572.45 第45课:函数或过程数组的输入和输出方法 592.46 第46课:INF0的用法602.47 第47课:多线程613跋75第3页共75页1 TCL 语法1.1 简介作为脚本语言, tcl 语法简单而功能强大。它诞生于 80

5、 年代初,进入中国的时间也许多年了。不过关 于 tcl 的资料多而杂,一时心血来潮,想到写一篇文章,以例子为中心,系统讲解tcl 语法,让技术人员花最少的时间对 tcl 有个全面而系统的了解,工作上使用时可以速查或参考代码。于是有了本文。1.2 运行环境多数代码运行在 ActiveTcl8.3.4.1-9.win32-ix86.exe 安装以后的 windows 环境中, 只有两个例程运行在 unix 的环境下。1.3 本文约定本文为了便于速查和速学, 所以每课分成讲解和例子两部分, 主要是围绕着例子进行讲解。 文字不多。1.4 参考资料本文主要参考了 TclTutor 2.0 beta4 。

6、2 引言2.1 第 1 课:简单文本输出讲解:1 注释符号是 或者 ;# , 在命令后注释用 ;# ,在行开头两者均可;2 puts :输出文本,多个单词如被空格或 TAB 分隔需要使用“”或 括起来; 3 多个命令 写在一行 使用 ; 间隔 。例子: 002_puts.tcl# ok ;# 正确;# ok;# 正确# ok;# 正确 , 分号和井号之间可以有空格;# 正确puts Hello,Worldputs Hello Worldputs Hello, World - In quotes puts Hello, World - In Bracesputs Hello;# 正确,多个单词之

7、间不是被空格或者 TAB 分隔开;# 这行命令运行出错,被空格分隔;# 注释# 这行命令运行出错,必须使用 ;# 作为注释符号puts This is line 1; puts this is line 2;# 正确,用分号分隔两个命令puts Hello, World; - With a semicolon inside the quotes;# 正确,分号在双引号内,作为字符串部分2.2 第 2 课:给变量赋值讲解:1. set:给变量赋值,格式为 set var value例子: 003_var.tcl;# 给变量 X 赋一个字符串set X This is a string;# 给变量

8、 Y 赋一个数字set Y 1.24;# 显示 X 和 Y 的内容puts $Xputs $Y;# 打印一个分隔串puts ;#打印在一行中,推荐使用双引号set label The value in Y is:puts $label $Yputs $label$Y2.3第3课:命令的赋值与置换一讲解:1 . TCL中命令的赋值分为置换和赋值两个步骤2. 续行符为3. 转义符同为4. 特殊字符列表:序号字符输出十八进制1a响铃x072b回车x083f清屏x0c4n换行x0a5r回车x0d6t制表符x097v垂直制表符(Vertical Tab)x0b8ddd八进制值d=0-79xhh十六进制值

9、h=0-9,A-F,a-f例子:004_eval.tcl;# Show how a affects the $set Z Alba nyset Z_LABEL The Capitol of New York is:puts $Z_LABEL $Z;#显示 Albanyputs $Z_LABEL $Z;#显示 $Z,被 转义;# The next line n eeds a backslash to escape the $puts nBen Franklin is on the $100.00 bill ;# n 换行;$100 前的 个变量,提示出错set a 100.00puts Wash

10、i ngton is not on the $a billputs Li ncoln is not on the $a bill 应该写为$aputs Hamilton is not on the $a billputs Ben Franklin is on the $a bill;# This is not what you want;#显示$100,说明是后结合的;#显示$a;#显示$100,说明是后结合的必须有,否则会将100作为-,先置换了 $a,此处严格的写 洗置换了 $a第82页共75页puts nexamplesof escape stri ngsputs TabtTabtTab

11、puts This string prints out non two lines;#行中没有打印出来,如果要打印出来,需要写成puts This stri ng comes outon a single line;#当一行太长,不便于阅读,使用做续行符2.4第4课:命令的赋值与置换二讲解:1 最外层是则不会进行置换操作,但其中的续行符仍然有效例子:005_escape.tclset Z Alba nyset Z_LABEL The Capitol of New York is:puts nexamples of differences between and ;#and前的双引号前必须有 进

12、行转义,否则这个双引号回和前面的双引号结合,导致成了 xxx” and “的结构,会提示出错puts $Z_LABEL $Z;# 显示 The Capitol of New York is: Albanyputs $Z_LABEL $Z;#显示$Z_LABEL $Z,没有进行置换,中不会置换puts nexamples of differe nces in n est ing and puts $Z_LABEL $Z;#最外层是双引号,所以进行了置换puts Who said, What this cou ntry needs is a good $Z cigar!? ;# 最外层是花括号,所

13、以没有进行置换puts nexamples of escape stri ngsputs There are no substituti ons done with in braces n r x0a f v;#puts But, the escaped n ewli ne at the end of astri ng is still evaluated as a space;#续行符仍然生效2.5第5课:命令的赋值与置换三讲解:1 可以传递其中的命令结果,注意不能被包含;2 双引号包含的中的命令可以正常执行,命令结果也可以传出; 3 包含的中的命令不会执行,更不会有命令结果传出来。例子:0

14、06_escape.tclset x abcputs A simple substitution: $xn;#显示 abcset y set x def;#先执行中的命令,将def赋值给x,然后将该命令的结果赋值给yputs Remember that set returns the new value of the variable: X: $x Y: $yn;#显示 x 禾口 y 者E是 defset z set x This is a string within quotes within braces;# 由于在中,所以并没有执行对 x 的赋值,只是将赋值给zputs Note the

15、 curly braces: $znset a set x This is a string within braces within quotes;#执行了对 x 的赋值操作,并将值传出来赋给了 aputs See how the set is executed: $aputs $x is: $xnset b set y This is a stri ng with in braces with in quotesputs Note the escapes the bracket:n $b is: $bputs $y is: $y2.6第6课:算数运算讲解:1. 操作符序号操作符解释1-+!

16、-:负号+ :正号 :位操作非!:逻辑非2* / %* :乘/ :除% :取模3+ -+ :力口 -:减4 :循环右移5& :按位与6AA :按位异或7| :按位或8& :逻辑与9II| :逻辑或10x?y:zif-the n-else2. 数学函数序号函数序号函数1acos11log102cos12tan3hypot13ata n24si nh14floor5asi n15pow6cosh16tanh7log17ceil8sqrt18fmod9ata n19sin10exp例子:007_math.tclset X 100;set Y 256;#行末是否有分号都可以set Z expr $Y

17、+ $X ”;#变量是否被双引号包含都可以,不过建议使用双引号set Z expr $Y + $Xset Z_LABEL $Y plus $X is puts $Z_LABEL $Zputs The square root of $Y is expr sqrt($Y)nputs Because of the precede nee rules 5 + -3 * 4 is: expr -3 * 4 + 5puts Because of the pare ntheses (5 + -3) * 4 is: expr (5 + -3) * 4puts nmore examples of differe

18、 nces betwee n and puts $Z_LABEL expr $Y + $X;#外层是花括号不会进行置换puts $Z_LABEL expr $Y + $X;#外层是双引号会进行置换puts The comma nd to add two nu mbers is: expr $a + $b2.7第7课:文本比较一 SWITCH 应用讲解:1. switch的分支中的命令使用花括号包含,但是并不会影响花括号中的命令执行,切记,这是switch的格式;2. 如果不想分支条件进行置换,需要在外加上花括号,不会影响分支中的命令执行。例子:008_switch.tcl;# Set the

19、variables well be compari ngset x ONE;set y 1;set z ONE;# This is legalswitch $x ONE puts ONE=1 TWO puts TW0=2 default puts NO_MATCH;#这种写法合法,但是阅读不便switch $x ONEputsONE=1 TWOputsTWO=2 defaultputsNO_MA TCH;#这种写法好看一些,推荐;#下面这种写法$z被置换,走入$z的条件分支,表面上看条件分支中的命令在花括号内,这只是switch的一种格式,所以其中的命令仍然被执行了。switch $x $zs

20、et y1 expr $y+1; puts MATCH $z. $y + $z is $y1 ONEset y1 expr $y+1; puts MATCH ONE. $y + one is $y1 TWOset y1 expr $y+2; puts 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 comma nd disable

21、s variable substituti on in the patter n;#下面为了不置换$z,在外层加上了花括号,于是走入了 ONE分支,而分支中的命令仍然被执行了 switch $x $zset y1 expr $y+1; puts MATCH $ z. $y + $z is $y1 ONEset y1 expr $y+1; puts MATCH ONE. $y + one is $y1TWOset y1 expr $y+2; puts MATCH TWO. $y + two is $y1THREE set y1 expr $y+3; puts MATCH THREE. $y +

22、three is $y1default puts $x is NOT A MA TCH2.8第8课:数值比较一IF应用讲解:1. 条件式结果FALSETRUE数值0非零yes / nonoyestrue / falsefalsetrue2 .置换变量的方法,set y x ; puts $y,因为是后结合并且是一次置换,所以打出来的是$x,不是$x的值;但是在if的条件式中进行了二次置换,$y被置换成了 $x的值3 注意:新行中需要写为 else ,不能将写到前一行的末尾,也不能省略后面的那个空格,后面的也需要写在当行,并且前面需要一个空格。例子:009_if.tcl;#判断是否相等使用=se

23、t 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 1if $x=1 puts GOT 1set y x;if ”$y != 1 puts $y is != 1 else puts $y is 1;#在if条件式中$y进行了二次置换;#在puts命令中,只进行了一次置换2.9第9课:WHILE 循环x讲解:1. while后面的条件表达式是放在花括号中的;例子:010_while.tclset x 1;while $x 5 puts x

24、is $x; set x expr $x + 1 puts exited first loop with X equal to $x nset x 0;while $x 6 break;if $x 3 continue;puts x is $x;puts exited sec ond loop with X equal to $x n放在双引号中会只执行一次置换;#只执行一次置换15,于是该条件永远为真;#如果去掉这句就成了死循环;#这句使4打不出来2.10第10课:FOR循环和incr讲解:1. incr x和set x expr $x + 1达到一样的效果,向上加一x例子:O11_for.

25、tclfor puts Start; set i 0 $i 2 incr i; puts I after incr: $i; ;# 第一部分只执行一次每次循环都会执行puts I in side first loop: $i,后面两部分for puts Start; set i 3 $i 2 incr i; puts I after incr: $i; ;# 不会执行循环体中的命令puts I in side sec ond loop: $iputs Start; set i 0;while $i 2 puts I in side first loop: $iincr i;puts I aft

26、er 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 + 3 is: sum 2 3nn;#调用过程#puts The sum of 2 + 3 is: sum 2 3nn;#出错,提示找不到第二个参数 ,置换过程中第一个参数是2 3,所以找不到第二

27、个参数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;#正确sum 2 3;#正确proc for a b c puts The for comma nd has bee n replaced by a puts;puts The argume nts were: $an$bn$cnfor set i 1 $i 10 incr i2.12第12课:过程 P

28、ROC的参数定义讲解:1. 过程的参数赋缺省值:proc name argl arg2 value2. 过程的不确定个数的参数定义:proc name argl args例子:013_proc.tclproc example first seco nd args ;#参数定义:赋缺省值和不确定个数参数定义if $seco nd = puts There is only one argume nt and it is: $first;return 1; else if $args = puts There are two argume nts - $first and $sec on d;ret

29、urn 2; else puts There are many arguments - $first and $second and $args;return man y;set cou nt1 example ONEset cou nt2 example ONE TWOset cou nt3 example ONE TWO THREE set cou nt4 example ONE TWO THREE FOURputs The example was called with $co un t1, $co un t2, $co un t3, and $co unt4 Argume nts2.1

30、3第13课:变量的作用域x讲解:1. 全局变量 定义: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 upvar $variable myvar;#此处variable只是一个参数名,可以修改为其他的来代替变量;# 此处也可写为 upvar 1 $variable myvarif $value = 0 puts $n

31、 ame starts with a lowercase letter n else puts $n ame starts with an uppercase letter n;#说明 string wordstart 禾口 string wordendset word 1 12 123;# 1的开始和结束位置,返回:0和1puts wordstart : stri ng wordstart $word 0puts worde nd : stri ng worde nd $word 0;#位置1上的空格的开始和结束位置,返回:1和2puts wordstart : stri ng wordst

32、art $word 1puts worde nd : stri ng worde nd $word 1;#位置2上所在单词12的开始和结束位置,返回: 2和4puts wordstart : stri ng wordstart $word 2puts worde nd : stri ng worde nd $word 2;#位置5上所在单词123的开始和结束位置,返回: 5和8puts wordstart : stri ng wordstart $word 5puts worde nd : stri ng worde nd $word 5;#位置6上所在单词123的开始和结束位置,返回: 5和

33、8puts wordstart : string wordstart $word 6puts worde nd : stri ng worde nd $word 62.佃第佃课:修改字符串函数讲解:1. 字符串函数序号函数解释1string tolower string1把string1转换为小与字母2string toupper string1把string1转换为大与字母3string trim string1 ? trimchars ?去掉string1前后的trimchars字符,如果不指定,缺省为空 格,trimleft 和 trimright 一样的情况4string trimleft string1? trimchars ?去掉string1左边的trimchars字符5string trimright string1? trimchars ?去掉string1右边的trimchars字符2. format 函数格式:format formatstri ng ?arg1 arg2

温馨提示

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

评论

0/150

提交评论