




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 工業園區的水質監測與管理研究
- 40人公司管理制度
- ab公司管理制度
- 標準公司內部管理制度
- 校區裝修現場管理制度
- 校園學生安全管理制度
- 校園廣播學生管理制度
- 校園疫情輿論管理制度
- 校園防控交通管理制度
- 6S現場管理考核制度
- 2024年河北特崗教師計劃招聘真題
- 2025年全國I卷作文講評
- 2024年中考二模 模擬卷 數學(江西卷)(參考答案及評分標準)
- 綜合辦公室考試題及答案
- 2025年中考押題預測卷:生物(福建卷01)(考試版)
- 北京開放大學2025年《裝配式技術與應用》形成性考核1答案
- 2025年恒豐銀行煙臺分行招聘筆試參考題庫含答案解析
- 【MOOC】工程電磁場與波-浙江大學 中國大學慕課MOOC答案
- ASTM-D3359-(附著力測試標準)-中文版
- FSSC22000V6.0體系文件清單
- 初高中數學銜接學情調查問卷(一)
評論
0/150
提交評論