




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、系統(tǒng)學(xué)習(xí)TCL腳本入門教程 版本:1. 0作者:次目 錄1TCL語法41.1簡介41.2運(yùn)行環(huán)境41.3本文約定41.4參考資料42引言52.1第1課:簡單文本輸出52.2第2課:給變量賦值52.3第3課:命令的賦值與置換一62.4第4課:命令的賦值與置換二72.5第5課:命令的賦值與置換三72.6第6課:算數(shù)運(yùn)算82.7第7課:文本比較SWITCH應(yīng)用92.8第8課:數(shù)值比較IF應(yīng)用102.9第9課:WHILE 循環(huán)112.10第10課:FOR循環(huán)和incr112.11第11課:過程PROC122.12第12課:過程PROC的參數(shù)定義132.13第13課:變量的作用域132.14第14課:L
2、IST結(jié)構(gòu)142.15第15課:LIST項(xiàng)的增刪改152.16第16課:更多LIST相關(guān)162.17第17課:字符串函數(shù)172.18第18課:更多字符串函數(shù)172.19第19課:修改字符串函數(shù)202.20第20課:正則表達(dá)式212.21第21課:更多正則表達(dá)式222.22第22課:數(shù)組242.23第23課:更多數(shù)組相關(guān)252.24第24課:文件存取282.25第25課:文件信息302.26第26課:TCL中的子進(jìn)程調(diào)用open & exec332.27第27課:命令或者變量是否存在info342.28第28課:解釋器狀態(tài)info352.29第29課:過程信息info362.30第30課
3、:模塊化source372.31第31課:建庫unknown & info library382.32第32課:創(chuàng)建命令eval402.33第33課:在eval中應(yīng)用format & list402.34第34課:不使用eval替換format & subst422.35第35課:改變工作目錄 cd & pwd432.36第36課:調(diào)試和錯(cuò)誤errorinfo & errorCode & catch442.37第37課:調(diào)試trace452.38第38課:命令行參數(shù)和環(huán)境串462.39第39課:time & unset472.40第40課
4、:socket & fileevent & vwait492.41第41課:日期時(shí)間clock512.42第42課:i/o通道fblocked & fconfig532.43第43課:子解釋器562.44第44課:數(shù)據(jù)庫操作572.45第45課:函數(shù)或過程數(shù)組的輸入和輸出方法592.46第46課:info的用法592.47第47課:多線程613跋731 TCL語法1.1 簡介作為腳本語言,tcl語法簡單而功能強(qiáng)大。它誕生于80年代初,進(jìn)入中國的時(shí)間也許多年了。不過關(guān)于tcl的資料多而雜,一時(shí)心血來潮,想到寫一篇文章,以例子為中心,系統(tǒng)講解tcl語法,讓技術(shù)人員花最少的時(shí)
5、間對(duì)tcl有個(gè)全面而系統(tǒng)的了解,工作上使用時(shí)可以速查或參考代碼。于是有了本文。1.2 運(yùn)行環(huán)境多數(shù)代碼運(yùn)行在 ActiveTcl 安裝以后的windows環(huán)境中,只有兩個(gè)例程運(yùn)行在unix的環(huán)境下。1.3 本文約定本文為了便于速查和速學(xué),所以每課分成講解和例子兩部分,主要是圍繞著例子進(jìn)行講解。文字不多。1.4 參考資料本文主要參考了TclTutor 2.0 beta4。2 引言2.1 第1課:簡單文本輸出講解:1注釋符號(hào)是 或者 ;# ,在命令后注釋用 ;# ,在行開頭兩者均可;2puts :輸出文本,多個(gè)單詞如被空格或TAB分隔需要使用“”或 括起來;3多個(gè)命令寫在一行使用 ; 間隔。例子:
6、002_puts.tcl# ok ;# 正確;# ok ;# 正確; # ok ;# 正確, 分號(hào)和井號(hào)之間可以有空格puts Hello ;# 正確puts Hello,World ;# 正確,多個(gè)單詞之間不是被空格或者TAB分隔開puts Hello World ;# 這行命令運(yùn)行出錯(cuò),被空格分隔puts "Hello, World - In quotes" ;# 注釋puts Hello, World - In Braces# 這行命令運(yùn)行出錯(cuò),必須使用 ;# 作為注釋符號(hào)puts "This is line 1" puts "this
7、is line 2" ;# 正確,用分號(hào)分隔兩個(gè)命令puts "Hello, World; - With a semicolon inside the quotes" ;#正確,分號(hào)在雙引號(hào)內(nèi),作為字符串一部分2.2 第2課:給變量賦值講解:1 set:給變量賦值,格式為 set var value例子:003_var.tcl;# 給變量X賦一個(gè)字符串set X "This is a string"# 給變量Y賦一個(gè)數(shù)字set Y 1.24;# 顯示X和Y的內(nèi)容puts $Xputs $Y;# 打印一個(gè)分隔串puts "."
8、# 打印在一行中,推薦使用雙引號(hào)set label "The value in Y is: "puts "$label $Y"puts $label$Y2.3 第3課:命令的賦值與置換一講解:1 TCL中命令的賦值分為置換和賦值兩個(gè)步驟2 續(xù)行符為 3 轉(zhuǎn)義符同為 4 特殊字符列表:序號(hào)字符輸出十六進(jìn)制1a響鈴x072b回車x083f清屏x0c4n換行x0a5r回車x0d6t制表符x097v垂直制表符(Vertical Tab)x0b8ddd八進(jìn)制值d=0-79xhh十六進(jìn)制值h=0-9,A-F,a-f例子:004_eval.tcl;# Show how
9、 a affects the $set Z "Albany"set Z_LABEL "The Capitol of New York is: "puts "$Z_LABEL $Z"#顯示Albanyputs "$Z_LABEL $Z" ;#顯示$Z,被 轉(zhuǎn)義;# The next line needs a backslash to escape the '$'puts "nBen Franklin is on the $100.00 bill" ;# n換行; $100前的 必須
10、有,否則會(huì)將100作為一個(gè)變量,提示出錯(cuò)set a 100.00puts "Washington is not on the $a bill" ;# This is not what you wantputs "Lincoln is not on the $a bill" ;# 顯示$100,說明是后結(jié)合的,先置換了$a,此處嚴(yán)格的寫應(yīng)該寫為 $aputs "Hamilton is not on the $a bill" ;# 顯示$aputs "Ben Franklin is on the $a bill" ;#
11、 顯示$100,說明是后結(jié)合的,先置換了$aputs "n. examples of escape strings"puts "TabtTabtTab"puts "This string prints out non two lines" ;# 行中 沒有打印出來,如果要打印出來,需要寫成 puts "This string comes outon a single line" ;# 當(dāng)一行太長,不便于閱讀,使用 做續(xù)行符2.4 第4課:命令的賦值與置換二講解:1最外層是 則不會(huì)進(jìn)行置換操作,但其中的續(xù)行符仍然有效
12、例子:005_escape.tclset Z "Albany"set Z_LABEL "The Capitol of New York is: "puts "n. examples of differences between " and " ;#and前的雙引號(hào)前必須有 進(jìn)行轉(zhuǎn)義,否則這個(gè)雙引號(hào)回和前面的雙引號(hào)結(jié)合, 導(dǎo)致成了 “xxx” and “ 的結(jié)構(gòu),會(huì)提示出錯(cuò)puts "$Z_LABEL $Z" ;# 顯示The Capitol of New York is: Albanyputs $Z_LA
13、BEL $Z ;# 顯示 $Z_LABEL $Z,沒有進(jìn)行置換,中不會(huì)置換puts "n. examples of differences in nesting and " "puts "$Z_LABEL $Z" ;# 最外層是雙引號(hào),所以進(jìn)行了置換puts Who said, "What this country needs is a good $Z cigar!"? ;#最外層是花括號(hào),所以沒有進(jìn)行置換puts "n. examples of escape strings"puts There are
14、 no substitutions done within braces n r x0a f v ;#puts But, the escaped newline at the end of astring is still evaluated as a space ;#續(xù)行符仍然生效2.5 第5課:命令的賦值與置換三講解:1 可以傳遞其中的命令結(jié)果,注意不能被 包含;2 雙引號(hào)包含的 中的命令可以正常執(zhí)行,命令結(jié)果也可以傳出;3 包含的 中的命令不會(huì)執(zhí)行,更不會(huì)有命令結(jié)果傳出來。例子:006_escape.tclset x "abc"puts "A simple
15、substitution: $xn" ;#顯示abcset y set x "def" ;#先執(zhí)行中的命令,將”def”賦值給x,然后將該命令的結(jié)果賦值給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" ;#由于在中,所以并沒有執(zhí)行對(duì)x的賦值,只是將賦值給zputs "N
16、ote the curly braces: $zn"set a "set x This is a string within braces within quotes" ;#執(zhí)行了對(duì)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 br
17、acket:n $b is: $b"puts "$y is: $y"2.6 第6課:算數(shù)運(yùn)算講解:1 操作符序號(hào)操作符解釋1- + !- : 負(fù)號(hào) + : 正號(hào) : 位操作非 ! : 邏輯非2* / %* : 乘 / : 除 % : 取模3+ -+ : 加 - : 減4<< >><< : 循環(huán)左移 >> : 循環(huán)右移5&& : 按位與6 : 按位異或7| : 按位或8&&&& : 邏輯與9| : 邏輯或10x?y:zif-then-else2 數(shù)學(xué)函數(shù)序號(hào)函數(shù)序號(hào)函數(shù)1
18、acos11log102cos12tan3hypot13atan24sinh14floor5asin15pow6cosh16tanh7log17ceil8sqrt18fmod9atan19sin10exp例子:007_math.tclset X 100;set Y 256 ;# 行末是否有分號(hào)都可以set Z expr "$Y + $X" ;# 變量是否被雙引號(hào)包含都可以,不過建議使用雙引號(hào)set Z expr $Y + $Xset Z_LABEL "$Y plus $X is "puts "$Z_LABEL $Z"puts &quo
19、t;The square root of $Y is expr sqrt($Y)n"puts "Because 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
20、 $Z_LABEL expr $Y + $X ;#外層是花括號(hào)不會(huì)進(jìn)行置換puts "$Z_LABEL expr $Y + $X" ;# 外層是雙引號(hào)會(huì)進(jìn)行置換puts "The command to add two numbers is: expr $a + $b"2.7 第7課:文本比較SWITCH應(yīng)用講解:1 switch的分支中的命令使用花括號(hào)包含,但是并不會(huì)影響花括號(hào)中的命令執(zhí)行,切記,這是switch的格式;2 如果不想分支條件進(jìn)行置換,需要在外加上花括號(hào),不會(huì)影響分支中的命令執(zhí)行。例子:008_switch.tcl;# Set the va
21、riables we'll be comparingset x "ONE"set y 1;set z "ONE"# This is legalswitch $x "ONE" "puts ONE=1" "TWO" "puts TWO=2" "default" "puts NO_MATCH" ;#這種寫法合法,但是閱讀不便switch $x "ONE" "puts ONE=1" "T
22、WO" "puts TWO=2" "default" "puts NO_MATCH" ;#這種寫法好看一些,推薦;#下面這種寫法$z被置換,走入$z的條件分支,表面上看條件分支中的命令在花括號(hào)內(nèi),這只是switch的一種格式,所以其中的命令仍然被執(zhí)行了。switch $x "$z"set y1 expr $y+1; puts "MATCH $z. $y + $z is $y1" "ONE"set y1 expr $y+1; puts "MATCH ONE.
23、 $y + one is $y1" "TWO"set 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 command disables variabl
24、e substitution in the pattern;#下面為了不置換$z,在外層加上了花括號(hào),于是走入了ONE分支,而分支中的命令仍然被執(zhí)行了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
25、is $y1" "THREE"set y1 expr $y+3; puts "MATCH THREE. $y + three is $y1" "default"puts "$x is NOT A MATCH" 2.8 第8課:數(shù)值比較IF應(yīng)用講解:1 條件式結(jié)果FALSETRUE數(shù)值0非零yes / nonoyestrue / falsefalsetrue2置換變量的方法,set y x ; puts $y ,因?yàn)槭呛蠼Y(jié)合并且是一次置換,所以打出來的是 $x ,不是$x的值;但是在if的條件式中進(jìn)行了二次
26、置換, $y 被置換成了 $x 的值3注意:新行中需要寫為 else ,不能將 寫到前一行的末尾,也不能省略 后面的那個(gè)空格,后面的 也需要寫在當(dāng)行,并且前面需要一個(gè)空格。例子: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"
27、;set y x;if "$y != 1" ;#在if條件式中$y進(jìn)行了二次置換 puts "$y is != 1" ;#在puts命令中,只進(jìn)行了一次置換 else puts "$y is 1" 2.9 第9課:WHILE 循環(huán)x講解:1while后面的條件表達(dá)式是放在花括號(hào)中的;放在雙引號(hào)中會(huì)只執(zhí)行一次置換例子:010_while.tclset x 1;while $x < 5 puts "x is $x" set x expr $x + 1puts "exited first loop wit
28、h X equal to $xn"set x 0;while "$x < 5" ;#只執(zhí)行一次置換1<5,于是該條件永遠(yuǎn)為真set x expr $x + 1if $x >6 break; ;#如果去掉這句就成了死循環(huán) if "$x > 3" continue; ;#這句使4 打不出來puts "x is $x" puts "exited second loop with X equal to $xn"2.10 第10課:FOR循環(huán)和incr講解:1incr x 和 set x e
29、xpr $x + 1 達(dá)到一樣的效果,向上加一x例子:011_for.tclfor puts "Start" set i 0 $i < 2 incr i; puts "I after incr: $i" ;#第一部分只執(zhí)行一次,后面兩部分每次循環(huán)都會(huì)執(zhí)行 puts "I inside first loop: $i" for puts "Start" set i 3 $i < 2 incr i; puts "I after incr: $i" ;#不會(huì)執(zhí)行循環(huán)體中的命令 puts &q
30、uot;I inside second loop: $i" puts "Start" set 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 調(diào)用方法中參數(shù)可以用花括號(hào)或者雙引號(hào)包含,也可以不包含3 在puts等命令中需要置換的話,需要使用方括號(hào)例子:012_proc.tclproc sum arg1 arg2 set
31、x expr $arg1+$arg2; return $x ;#過程返回值 puts " The sum of 2 + 3 is: sum 2 3nn" ;#調(diào)用過程#puts " The sum of 2 + 3 is: sum 2 3nn" ;#出錯(cuò),提示找不到第二個(gè)參數(shù),置換過程中第一個(gè)參數(shù)是2 3,所以找不到第二個(gè)參數(shù)puts " The sum of 2 + 3 is: sum(2 3)nn" ;#輸出sum(2 3),因?yàn)闆]有方括號(hào),根本沒有進(jìn)行置換puts " The sum of 2 + 3 is: sum2
32、 3nn" ;#輸出sum2 3,因?yàn)闆]有方括號(hào),根本沒有進(jìn)行置換sum 2 3 ;#正確sum 2 3 ;#正確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的參數(shù)定義講解:1 過程的參數(shù)賦缺省值:proc name arg1 a
33、rg2 value2 過程的不確定個(gè)數(shù)的參數(shù)定義:proc name arg1 args例子:013_proc.tclproc example first second "" args ;#參數(shù)定義:賦缺省值和不確定個(gè)數(shù)參數(shù)定義 if $second = "" puts "There is only one argument and it is: $first" return 1; else if $args = "" puts "There are two arguments - $first and
34、$second" return 2; else puts "There are many arguments - $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,
35、 $count3, and $count4 Arguments"2.13 第13課:變量的作用域x講解:1 全局變量定義:global var12 局部變量:upvar x y 等同于upvar 1 x y,作用有兩個(gè):一是將上一層的x的值賦給y;二是將上一層的x的地址賦給y,于是修改y等于修改x。1代表作用范圍,也可為2,3等,不能為0例子:014_varscope.tclproc SetPositive variable value ;#此處variable只是一個(gè)參數(shù)名,可以修改為其他的來代替變量 upvar $variable myvar ;#此處也可寫為upvar 1 $v
36、ariable myvar if $value < 0 set myvar expr -$value; else set 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 varia
37、ble to 1; set a 2;# Set x, two layers up to 2; ;# A first level 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 on
38、e y;# Call one, and output X and Y after the call.puts "nX: $x Y: $y"2.14 第14課:LIST結(jié)構(gòu)講解:1 list結(jié)構(gòu)下標(biāo)是從零開始的,引用方式是lindex list 位置-12 字符串可以使用 :split 字符串 分隔符 拆分得到一個(gè)list,缺省分隔符是空格3 list可以直接定義:set z list a b4 foreach x $list :用以列出list中的所有項(xiàng)5 llength $list :用以列出list中的項(xiàng)數(shù)例子:015_list.tclset x "a b c
39、"puts "Item 2 of the list $x is: lindex $x 2n" ;#引用list的第三項(xiàng)(從0開始)的值c使用函數(shù)lindex,并使用方括號(hào)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 $
40、y" ;#此處的list z是兩個(gè)項(xiàng):puts和” arg 2 is $y”puts "A command resembles: $zn" ;#這里的輸出是:A command resembles: puts arg 2 is 7 4 1776,注意:1。當(dāng)有多個(gè)子項(xiàng)時(shí)自動(dòng)使用了花括號(hào)來明確是一項(xiàng);2。由于是雙引號(hào)保護(hù),對(duì)$y進(jìn)行了置換set i 0;foreach j $x ;#注意這里是foreach j $x 而不是 foreach $j $x puts "$j is item number $i in list x" incr i; 2
41、.15 第15課:LIST項(xiàng)的增刪改講解:1 在中執(zhí)行的命令不會(huì)改變其中變量的值,在外面單獨(dú)執(zhí)行會(huì)改變其值;2 list函數(shù)列表:序號(hào)函數(shù)解釋1concat ?arg1 arg2 .argn合并list2lappend listname ?arg1 arg2 .argn在list后增加項(xiàng)3linsert listname index arg1 ?arg2 .argn在list中插入項(xiàng)4lreplace listname first last ?arg1 arg2 .argn替代list中的項(xiàng)例子:016_list.tclset b list a b c d e f g h ;#為4項(xiàng):a; b
42、; c d e;f g hputs "Treated as a list: $bn"set b split "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去掉了第一層花括號(hào),輸出:a b c d e f g hlappend a ij K lm;#在a后面增加了一項(xiàng)ij K lm,注意此處a的值改變了lapp
43、end a ij K lm;#在a后面增加了三項(xiàng) ij K lm,注意此處a的值改變了puts "After lappending: $an"set b linsert $a 3 "1 2 3" ;# 在a的第三項(xiàng)(0開始數(shù))插入一項(xiàng)”1 2 3” ,注意此處a的值并沒有被改變set b linsert $a 4 1 2 3 ;# 在a的第三項(xiàng)(0開始數(shù))插入一項(xiàng)1 2 3,注意此處a的值并沒有被改變set b linsert $a 1 1 2 3 ;# 在a的第三項(xiàng)(0開始數(shù))插入三項(xiàng) 1 2 3,注意此處a的值并沒有被改變puts "Aft
44、er linsert at position 3: $bn"# "AA" and "BB" are two list elements.set b lreplace $b 3 5 "AA" "BB" ;#注意是第三到五項(xiàng)(0開始數(shù))被替換puts "After lreplacing 3 positions with 2 values at position 3: $bn"2.16 第16課:更多LIST相關(guān)講解:1 list相關(guān)的函數(shù)列表:序號(hào)函數(shù)解釋1lsearch list pat
45、tern按照某種模式查找list中的項(xiàng),返回滿足條件的第一項(xiàng)的出現(xiàn)位置2lsort list對(duì)list排序3lrange list first last從list中取出一個(gè)范圍的項(xiàng)lsort -mode list排列列表。-mode : -ascii-dictionary 與acsii類似,只是不區(qū)分大小寫-integer 轉(zhuǎn)化為整數(shù)再比較-real 轉(zhuǎn)化為浮點(diǎn)數(shù)再比較-command command 執(zhí)行command來做比較2 通配符列表序號(hào)通配符解釋1*代表任意字符2?代表一個(gè)字符3X轉(zhuǎn)義符4.代表一個(gè)集合例子:017_list.tclset list1 list a b cset bp
46、os lsearch $list1 bputs "b position : $bpos" ;#返回位置值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* ;#返回滿足條件的第一項(xiàng)的位置3#set x lsearch $list 17* ;#返回-1,沒有找
47、到滿足條件的項(xiàng)#set y lsearch $list 180? ;#返回-1,沒有找到滿足條件的項(xiàng)incr x; incr 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
48、0 " set x lsearch $list Madison*set srtlist lsort $list;set y lsearch $srtlist Madison*;puts "n$x Presidents came before Madison chronologically"puts "$y Presidents came before Madison alphabetically"2.17 第17課:字符串函數(shù)講解:1字符串函數(shù)列表序列函數(shù)解釋1string length返回字符串的長度2string index返回字符串相應(yīng)位
49、置的字符3string range返回字符串中一個(gè)范圍內(nèi)的字符子串例子:018_string.tclset string "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 $
50、string 5 10" are characters between the 5'th and 10'th" ;#返回”is my ”2.18 第18課:更多字符串函數(shù)講解:1字符串函數(shù)列表序號(hào)函數(shù)解釋1string compare string1 string2字符串比較返回:-1 :string1比string2小0 :string1和string2相等1 :string1比string2大2string first string1 string2返回string1在string2中第一次出現(xiàn)的位置;如果string2不在string1中,返回-13s
51、tring last string1 string2返回string1在string2中最后一次出現(xiàn)的位置;如果string2不在string1中,返回-14string wordstart string1 index返回string1中index處的單詞的開始位置5string wordend string1 index返回string1中index處的單詞的結(jié)束位置6string match pattern string1返回string1中是否滿足匹配模式pattern匹配模式的通配符:* :任意字符? :單個(gè)字符X :轉(zhuǎn)義符. :字符區(qū)間,例如:a-z例子:019_stringcmp.
52、tclset fullpath "/usr/home/clif/TCL_STUFF/TclTutor/Lsn.17"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 "/" $
53、path;#根據(jù)開頭是否是 來判斷是相對(duì)路徑還是絕對(duì)路徑 if $first != 0 puts "$path is 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 ;#
54、report the last word. incr last; if $last != string length $path 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格式的區(qū)分,lsearch list pattern,匹配模式是在后面 puts "$path is part of the source code control tree" ;#判斷一個(gè)名字開頭是大寫還是小寫字母 set comparison string compa
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 加工承攬意向合同范本
- 出讓合同范本
- 買磚合同范本
- 兒女撫養(yǎng)合同范本
- 農(nóng)村租房建基站合同范本
- 代建合同范本政府蓋章
- 世界500強(qiáng)合同范本
- 會(huì)務(wù)代辦合同范本
- 供貨定金合同范本
- 別墅門窗出售合同范本
- 2024至2030年中國毛絨玩具數(shù)據(jù)監(jiān)測研究報(bào)告
- 建筑復(fù)工復(fù)產(chǎn)安全培訓(xùn)
- GB 21258-2024燃煤發(fā)電機(jī)組單位產(chǎn)品能源消耗限額
- 八年級(jí)上學(xué)期語文12月月考試卷
- 醛固酮增多癥與原發(fā)性醛固酮增多癥概述
- 山東省淄博市2023-2024學(xué)年高一下學(xué)期期末教學(xué)質(zhì)量檢測數(shù)學(xué)試題
- 廣東省2024年普通高中學(xué)業(yè)水平合格性考試語文仿真模擬卷01(解析版)
- 2025屆新高考生物精準(zhǔn)復(fù)習(xí)+提高農(nóng)作物產(chǎn)量
- 第6課歐洲的思想解放運(yùn)動(dòng)教學(xué)設(shè)計(jì)2023-2024學(xué)年中職高一下學(xué)期高教版(2023)世界歷史
- 2024年云南省昆明市選調(diào)生考試(公共基礎(chǔ)知識(shí))綜合能力題庫必考題
- 2024年時(shí)政試題庫(奪分金卷)
評(píng)論
0/150
提交評(píng)論