版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、系统学习TCL脚本入门教程版本:1. 0作者:次1TCL语法1.1 简介41.2 运行环境41.3 本文约定41.4 参考资料42弓I言2.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勺参数定义 13
2、2.13 第13课:变量的作用域 132.14 第 14 课:LIST 结构142.15 第15课:LIST项的增删改152.16 第16课:更多 LIST相关 162.17 第17课:字符串函数 172.18 第18课:更多字符串函数 182.19 第19课:修改字符串函数 202.20 第20课:正则表达式 222.21 第21课:更多正则表达式 232.22 第22课:数组252.23 第23课:更多数组相关 262.24 第24课:文件存取 292.25 第25课:文件信息 312.26 第26课:TCL中的子进程调用 OPEN& EXEC342.27 第27课:命令或者变量是否存在I
3、NFO352.28 第28课:解释器状态info362.29 第29课:过程信息INFO372.30 第 30 课:模块化SOURCE382.31 第 31 课:建库UNKNOW& INFO LIBRARY 392.32 第32课:创建命令 EVAL.412.33 第 33 课:在 EVAL中应用 FORMAT& LIST 412.34 第 34 课:不使用 EVAL替换FORMAT& SUBST432.35 第35课:改变工作目录 CD & PWD442.36 第 36 课:调试和错误 ERRORINFC& ERRORODE & CATCH452.37 第 37 课:调试TRACE.462.
4、38 第38课:命令行参数和环境串 482.39 第 39 课:TIME & UNSET482.40 第 40 课:SOCKET& FILEEVENT &VWAIT 502.41 第41课:日期时间 CLOCK522.42 第 42 课:I/O 通道FBLOCKED& FCONFIG 542.43 第43课:子解释器 572.44 第44课:数据库操作 582.45 第45课:函数或过程数组的输入和输出方法 602.46 第46课:INFO的用法 612.47 第47课:多线程623跋761 TCL语法1.1简介作为脚本语言,tel语法简单而功能强大。它诞生于80年代初,进入中国的时间也许多年
5、了。不过关于tel的资料多而杂,一时心血来潮,想到写一篇文章,以例子为中心,系统讲解tel语法,让技术人员花最少的时间对tel有个全面而系统的了解,工作上使用时可以速查或参考代码。于是有了本文。1.2运行环境多数代码运行在 ActiveTcl8.341-9.win32-ix86.exe安装以后的 windows环境中,只有两个例程运行在unix的环境下。1.3本文约定本文为了便于速查和速学, 所以每课分成讲解和例子两部分,主要是围绕着例子进行讲解。文字不多。1.4参考资料本文主要参考了TclTutor 2.0 beta4 。2引言2.1第1课:简单文本输出讲解:1. 注释符号是 #或者;# ,
6、在命令后注释用;# ,在行开头两者均可;2. puts :输出文本,多个单词如被空格或TAB分隔需要使用“”或括起来;3. 多个命令写在一行使用;间隔。例子:002_puts.tcl# ok;#;# ok;#;# ok;#puts Hello;#puts Hello,World;#puts Hello World;#puts Hello, World - In quotes ;# puts Hello, World - In Braces #正确正确正确,分号和井号之间可以有空格正确正确,多个单词之间不是被空格或者TAB分隔开这行命令运行出错,被空格分隔注释这行命令运行出错,必须使用;#作为注
7、释符号puts This is line 1; puts this is line 2;#正确,用分号分隔两个命令puts Hello, World; - With a semico Ion in side the quotes ;#正确,分号在双引号,作为字符串一部分2.2第2课:给变量赋值讲解:1. set :给变量赋值,格式为set var value例子:003_var.tcl;#给变量X赋一个字符串set X This is a stri ng;#给变量Y赋一个数字set Y 1.24;#显示X和Y的容puts $Xputs $Y;#打印一个分隔串puts ;#打印在一行中,推荐使用
8、双引号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垂直 制表符(VerticalTab)x0b8ddd八进制值d=0-79xhh十六进制值h=0-9,A-F,a-f例子:004_eval.tcl;# Show how a affects the $set Z Alba nyset Z_LA
9、BEL 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 前的 必须有,否则会将100作为一个变量,提示出错set a 100.00puts Washi ngton is not on the $a bill ;# This is not what you wantputs
10、 Lincoln is noton the $a bill ;#显示 $100,说明是后结合的,先置换了 $a,此处严格的写应该写为$aputs Hamilt on is not on the $a bill;#显示 $aputs Ben Franklin is on the $a bill ;#显示 $100,说明是后结合的,先置换了 $aputs nexamplesof escape stri ngsputs TabtTabtTabputs This string prints out non two lines ;#行中 没有打印出来,如果要打印出来,需要写成puts This stri
11、 ng comes outon a si ngle line;#当一行太长,不便于阅读,使用 做续行符2.4第4课:命令的赋值与置换二讲解:1 最外层是则不会进行置换操作,但其中的续行符仍然有效例子:005_escape.tclset Z Alba nyset Z_LABEL The Capitol of New York is:puts nexamples of differe nces betwee n and ;#a nd前的双弓 I号前必须有进行转义,否则这个双引号回和前面的双引号结合,导致成了 “xxx” and “的结构,会提示出错puts $Z_LABEL $Z;#显示 The
12、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!? ;#最外层是花括号,所以没有进行置换puts nexamples of escape stri ngsputs There are no substituti ons
13、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 包含的中的命令不会执行,更不会有命令结果传出来。例子:006_escape.tclset x abcputs A simple substitution: $xn ;#显示 abcset y set x
14、 def;#先执行中的命令,将” 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: $znset a set x This is a string within braces within quotes
15、;#执行了对 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-+!-:负号+:正号 :位操作非!:逻辑非2* / %* :乘/ : 除% :取模3+ -+ :加-:减4 :循环右移5& :按位与6AA :按位异或7|
16、:按位或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 + $X;#变量是否被双引号包含都可以,不过建议使用双引号set Z expr $Y + $Xset Z_LABEL $Y plus $X is puts $
17、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 nces betwee n and puts $Z_LABEL expr $Y + $X;#外层是花括号不会进行置换puts $Z_LABEL expr $Y
18、+ $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 variables well be compari ngset x ONE;set y 1;set z ONE;# This is legalswitch $x ON
19、E puts ONE=1 TWO puts TW0=2 default puts NO_MATCH ;#这种写法合 法,但是阅读不便switch $x ONEputs ONE=1 TWOputs TWO=2 这种写法好看一些,推荐defaultputs NO_MATCH;#;#下面这种写法$z被置换,走入$z的条件分支,表面上看条件分支中的命令在花括号,这只是switch的一种格式,所以其中的命令仍然被执行了。switch $x $zsety1expr $y+1;putsMATCH $ z. $y + $z is $y1 ONEset y1expr $y+1;putsMATCH ONE. $y
20、 + one is $y1 TWOset y1expr $y+2;putsMATCH TWO. $y + two is $y1 THREEset y1expr $y+3;putsMATCH THREE. $y + three is $y1 default puts $x does not match any of these choices;# This form of the comma nd disables variable substituti on in the patter n;#下面为了不置换$z,在外层加上了花括号,于是走入了 ONE分支,而分支中的命令仍然被执行了switch
21、 $x $zONETWOset y1 expr $y+1; puts MATCH $ z. $y + $z is $y1 set y1 expr $y+1; puts MATCH ONE. $y + one is $y1set y1 expr $y+2; puts MATCH TWO. $y + two is $y1THREE set y1 expr $y+3; puts MATCH THREE. $y + three is $y1default puts $x is NOT A MATCH2.8第8课:数值比较一IF应用讲解:1.条件式结果FALSETRUE数值0非零yes / nonoye
22、strue / falsefalsetrue2 .置换变量的方法,set y x ; puts $y,因为是后结合并且是一次置换,所以打出来的是$x ,不是$x的值;但是在if的条件式中进行了二次置换,$y 被置换成了 $x的值3 注意:新行中需要写为 else ,不能将写到前一行的末尾,也不能省略后面的那个空格,后面的也需要写在当行,并且前面需要一个空格。例子:009_if.tclset x 1;if $x = 2 puts $x is 2 else puts $x is n ot 2 ;#判断是否相等使用if $x != 1 ;#puts $x is != 1 else puts $x i
23、s 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 is $x; set x expr $x + 1puts exited first loop with X equal to $x nset x 0;while
24、$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例子:011_for.tclfor puts Start; set i 0 $i 2 incr i; puts I after incr: $i; ;#第一部分只执行一次,后面两部分每次循环都会执行puts I in si
25、de first loop: $ifor 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 after incr: $i;2.11第11课:过程PROC讲解:1. 格式:proc name args body2. 调用方法中参数可以用花括号或者双引号包含,也可以不包含3. 在puts等命令中需要置换的
26、话,需要使用方括号例子: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,所以找不到第二个参数puts The sum of 2 + 3 is: sum(2 3)nn;#行置换puts The sum of 2 + 3 is: sum2 3 nn;#行置换调用过程出错,提示找不到第二个参数,置换过程中第输出sum(2 3),因为没有方括号
27、,根本没有进输出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课:过程PROC勺参数定义讲解:1. 过程的参数赋缺省值:proc name arg1 arg2 value2. 过程的不确定个数的参数定义:proc name arg1 args例子:013_proc.tclpr
28、oc example first seco nd args ;#参数定义:赋缺省值和不确定个数参数定义if $seco nd = puts There is only one argument and it is: $first;return 1; else if $args = puts There are two argume nts - $first and $sec on d;return 2; else puts There are many arguments - $first and $second and $args;return ma ny;set cou nt1 examp
29、le 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.13第13课:变量的作用域x讲解:1.全局变量定义:global var12.局部变量:upvar x y 等同于upvar 1 x y ,作用有两个:一是将上一层的 x的值赋给y;二是将 上一层的x的地址赋
30、给y,于是修改y等于修改x。1代表作用围,也可为 2,3等,不能为0例子:014_varscope.tclproc SetPositive variable value ;#此处variable只是一个参数名,可以修改为其他的来代替变量upvar $variable myvar;#此处也可写为 upvar 1 $variable myvarif $value = 0 puts $n ame starts with a lowercase letter n else puts $n ame starts with an uppercase letter n;# 说明 string wordsta
31、rt 和 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 wordstart $word 1puts worde nd : stri ng worde nd $word 1;#位置2上所在单词12的开始和结束位置,返回: 2和4puts wordstart : stri ng word
32、start $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和8puts wordstart : string wordstart $word 6puts worde nd : stri ng worde nd $word 62.19第19课:修改字符串函数讲解:1.字符串函数
33、序号函数解释1string tolower string1把string1转换为小与字母2string toupper string1把string1转换为大与字母3stri ngtrimstri ng1?trimchars ?去掉string1 前后的trimchars字符,如果不指定,缺省为空格,trimleft和trimright 一样的情况4stri ngtrimleftstri ng1?trimchars ?去掉string1 左边的trimchars 字符5stri ngtrimrightstri ng1?trimchars ?去掉string1 右边的trimchars 字符2. format 函数格式:format formatstring ?arg1 arg2 . argn 注意是 format,不是 string format格式串列表:序号格式描述1s字符串2d十进制整数3x十六进制数值4o八进制数值5f浮点数6-左对齐7+右对齐,不指定-或+,缺省是右对齐
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 宿舍消音活动策划方案(3篇)
- 晚会活动策划方案步骤(3篇)
- 电影分享策划活动方案(3篇)
- 独居女孩活动策划方案(3篇)
- 如何策划菜单活动方案(3篇)
- 施工方案台账全套(3篇)
- 校区跨年活动方案策划(3篇)
- 2025年大学土壤肥料(施用技术实操)试题及答案
- 2025年中职电气(电气测量基础)试题及答案
- 2025年大学大三(工商管理)人力资源管理阶段测试试题及答案
- 病媒生物防制服务外包 投标方案(技术方案)
- 光伏电站-强制性条文执行检查表
- 经济学在生活中
- 产品防护控制程序培训课件
- ISO-6336-5-2003正齿轮和斜齿轮载荷能力的计算-第五部分(中文)
- 轨道线路养护维修作业-改道作业
- 2023-2024学年上海市闵行区四上数学期末综合测试试题含答案
- 中铝中州矿业有限公司禹州市方山铝土矿矿山地质环境保护和土地复垦方案
- 解除劳动合同证明电子版(6篇)
- 呼吸科规培疑难病例讨论
- 基于PLC控制的小型钻床机械设计
评论
0/150
提交评论