Tcl編程簡介(五)

來源:互聯網
上載者:User

Tcl編程簡介(五)
 

lsort -mode list

排列列表。

-mode : -ascii

  -dictionary 與acsii類似,只是不區分大小寫

  -integer 轉化為整數再比較

  -real 轉化為浮點數再比較

  -command command 執行command來做比較

open fileName
open fileName access

 

  開啟檔案,返回一個檔案描述符。

access
r w a r+ w+ a+

 

  定義與C中相同。如檔案名稱的第一個字元為|表示一管道的形式來開啟。

set f [open |more w]
set f [open /etc/pass r]

proc name args body

 

  建立一個新的過程,可以替代任何存在的過程或命令。

proc wf {file str} {
puts -nonewline $file str
flush $file
}
set f [open /tmp/a w]
wf $f "first line "
wf $f "second line "

 

  在函數末尾可用 return 來傳回值。

puts -nonewline fileId string

  向fileId中寫入string,如果不加上 -nonewline 則自動產生一個分行符號。

pwd

  返回目前的目錄。

read fileId
read fileId numBytes

 

  從fileId中讀取numBytes個位元組。

regexp ?switches? exp string ?matchVar? ?subMatchVar
subMatchVar ...?

 

  執行Regex的匹配。

?switches? -nocase 不區分大小寫
-indices 返回匹配區間

 

  如:

regexp ^abc abcjsdfh
//return 1
regexp ^abc abcjsdfh a
//return 1
puts $a
//return abc

regexp -indices ^abc abcsdfjkhsdf a
//return 1
puts $a
//return "0 2"

regsub ?switchs? exp string subSpec varName

  執行Regex的替換,用subSpec的內容替換string中匹配exp的部分。

  ?switchs? -all 將所有匹配的部分替換,預設子替換第一個,傳回值為替換的個數。

  -nocase 不區分大小寫。

  如:

regsub abc abcabcbac eee b
//return 1
puts $b
//return "eeeabcabc"

regsub -all abc abcabcabc eee b
//return 3
puts $b
//return "eeeeeeeee"

return
立即從當前命令中返回。
proc ff {} {
return friday
}

set a [ff]
//a = "friday"

scan string `format" varname ...

  從string中安format來讀取值到varname。

seek fileId offset ?origin?

  移動檔案指標。

origin: start current end

  offset從哪裡開始算起。

set varname ?value?

  設定varname用value,或返回varname的值。如果不是在一個proc命令中則產生一個全域變數。

source fileName

  從filename中讀出內容傳給Tcl解釋起來執行。

split string ?splitChars?

  將string分裂成列表。預設以空白為分隔字元,也可通過splitChars來設定分隔字元

string subcommand arg ...

  用於字串的命令。

string compare string1 string2

  執行字串的比較,按 C strcmp 的方式。返回 -1, 0, or 1。

string first string1 string2

  在string1種尋找string2的定義次出現的位置。未找到返回-1。

string length string

  返回字串string的長度。

string match pattern string

  判斷string是否能匹配pattern。pattern是以shell檔案名稱的統配格式來給出。

string range string first last

  返回字串string中從first到last之間的內容。

string tolower string

  將string轉換為小寫。

string toupper string

  將string轉換為大寫。

string trim string

  將string的左右空白去掉。

string trimleft string

  將string的左空白去掉。

string trimright string

  將string的右空白去掉。

tell fileId

  返回fileId的檔案指標位置。

time command

  執行命令,並計算所消耗的時間。

time "ls --color"
some file name
503 microseconds per iteration
trace subcommand
trace subcommand arg ...

 

  監視變數的儲存。子命令定義了不少,但目前只實現了

virable。
trace variable name ops command
name 為變數的名字。
ops 為要監視的操作。
r 讀
w 寫
u unset

 

  command 條件滿足時執行的命令。

  以三個參數來執行 name1 name2 ops name1時變數的名字。當name1為向量時,name2為下標,ops為執行的操作。

  例如:

proc ff {name1 name2 op} {
puts [format "%s %s %s" name1 name2 op]
}
set a hhh
trace variable a r {ff}
puts $a
//return "a r hhh"

unknown cmdName

 

  unknown 並不是 Tcl 的一部分,當 Tcl 發現一條不認識的命令時會看看是否存在 unknown命令,如果有,則調用它,沒有則出錯。

  如:

#!/usr/bin/tclsh
proc unknown {cwd args} {
puts $cwd
puts $args
}
//下面是一條錯誤命令
sdfdf sdf sdkhf sdjkfhkasdf jksdhfk
//return "sdfdf sdf sdkhf sdjkfhkasdf jksdhfk"

unset name ...

  刪除一個或多個變數(標量或向量)。

uplevel command ...

  將起參數串連起來(象是在concat中)。最後在由level所指定的上下文中來執行。如果level是一個整數,給出了在棧中的距離(是跳到其它的命令環境中來執行)。預設為1(即上一層)。

  如:

#!/usr/bin/tcl
proc ff {} {
set a "ff" //設定了局部的a
-------------------------
}
set a "global"
ff
puts $a
//return "global"

 

  再看下一個:

#!/usr/bin/tcl
proc ff {} {
uplevel set a "ff" //改變上一級棧中的a
-------------------------------------
}
set a global
ff
puts $a
//return "ff"

 

  如果level是以#開頭後接一個整數,則level指出了在棧中的絕對位置。如#0表示了頂層(top-level)。

  a b c 分別為三個命令,下面是它們之間的調用關係,

top-level -> a -> b -> c -> uplevel level

  絕對位置: 0 1 2 3

  當level為 1 或 #2 都是在 b 的環境中來執行。

  3 或 #0 都是在 top-level 的環境中來執行。

upvar ?level? otherVar myVar ?otherVar myVar ...?

  在不同的棧中為變數建立串連。這裡的level與uplevel中的level是同樣風格的。

  例如:

#!/usr/bin/tcl
proc ff {name } {
upvar $name x
set x "ff"
}
set a "global"
ff a
puts $a
//return "ff"

while test body

 

  舉個例子吧:

set x 0
while {$x<10} {
puts "x is $x"
incr x
}

 

  Built-in variables 內建的變數下名的全域變數是由 Tcl library 自動來管理的。一般是唯讀。

env

  環境變數數組。

  如:

puts $env(PATH)
// return /bin:/usr/bin:/usr/X11R6/bin

errorCode

 

  當錯誤發生時儲存了一些錯誤資訊。用下列格式來儲存:

CHILDKILLED pid sigName msg

  當由於一個訊號而被終止時的資訊。

CHILDSTATUS pid code

  當一個子程式以非0值退出時的格式。

CHILDSUSP pid sigName msg

  當一個子程式由於一個訊號而被終止時的格式。

NONE

  錯誤沒有附加資訊。

UNIX errName msg

  當一個核心調用發生錯誤時使用的格式。

errorInfo

  包含了一行或多行的資訊,描述了錯誤發生處的程式和資訊。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.