Tcl和C语言的区别在于:Tcl中所有的控制结构都是由相应的命令实现,而C语言中则是一条控制语句。
1、if else命令
set x hello
if {![string compare $x hell]} {
puts "string is hell"
} elseif ![string compare $x hel] {
puts "string is hel"
} elseif ![string comapre $x hello] {
puts "string is hello"
} else {
puts "error input string"
}
注意结构体的花括号要跟着if语句一行,否则认为该行执行结束。
2、for命令
语法:for{start} {test expr} {next}{
body
}
其中next是一个后置条件,即执行完循环体body后执行next。
for {set i 0} {$i<10} {incr i 2} {
if {$i ==4} {
continue
}
puts "i=$i"
if {$i>=6} {
break
}
}
break命令导致立刻从循环体退出。continue表示忽视后面未执行的循环体然后跳到下一个循环里面。
i = 0
i = 2
i = 6
3、while命令
语法:while {test} {
body }
while {$i > 0} {
puts "Current index is $i."
incr i –1
}
=> Current index is 3.
Current index is 2.
Current index is 1.
和for循环的区别:for循环需要在next里更改被检测体next的值,而while需要在循环体内更改被检测体test的值。
4、switch命令:
通过将给定字符串与不同的匹配模式进行匹配从而选择执行多分支命令体
语法:switch [option] string {
pattern-1 {body}
pattern-2 {body}
pattern-3 {body}
}
其中option:-exact 精确匹配(默认)、-glob用glob格式匹配、-regexp用正则表达式匹配、--标记选项结束或者说明不用选项。
set result TRUE
switch -exact $result {
"TRUE" {
# Comment2: This comment is ok
puts "TRUE"
}
"FALSE" {
puts "FALSE"
}
"UNKNOW" - #unknow与default一样,可以不加
default { puts "UNKNOW or unkown value"}
}
5、catch命令
用来捕获调用错误。
语法:catch {com_proc} ?res?
其中catch有两个变元,com_proc是命令体,res用来保存返回结果。如果有错,catch会返回1,无错返回0;
set status [catch { puts "The value of y is $y"} res]
=>1 ;#因为用了未定义的变量 y 而出错 ,catch返回1代表出错,此时res存储错误信息。
if {$status} {
puts "Command faild. Error Info: $res"
}
=>Command faild. Error Info: can't read "y": no such variable
proc test {} {
return OK
}
set status [catch {test} res]
=> 0
puts $res ;#catch 命令还可以捕获函数返回值
=> OK
6、error命令(不太懂)
报告错误信息并停止脚本运行。
语法: error message_string ?info? ?error_code?
message_string 是错误信息字符串,info 变元用于初始化全局变量 errorInfo,如果 info 没
有提供,则 error 自身初始化 errorInfo。变元 code 指定了一个机器可读的错误信息,会被存
储在全局变量 errorCode 中,默认为 NONE。
#定义一个函数 foo,返回一条错误:
%proc foo {} {
error "1.Function foo report error" "2.Some error in function foo" 20
}
% foo ;#运行 foo 函数
=> 1.Function foo report error
% catch {foo} str
=>1 ;#用 catch 捕获错误信息,error 输出的错误信息被保存在了 str
中
% puts $res
=> 1.Function foo report error
% puts $errorInfo ;#显示 errorInfo 内的内容
=> Some error in function foo
(procedure "foo" line 1)
invoked from within
"foo" ;#errorInfo 中给出了详细的出错报告,每条执行错误的
;#命令都会引起 errorInfo 的内容被改写
% puts $errorCode ;#显示 errorCode 的值,为 error 报告的 code 值
=> 20
% puts $a ;#执行一条错误命令:试图输出一个没有定义变量的值
=> can't read "a": no such variable ;#返回的错误信息
% puts $errorInfo
=>can't read "a": no such variable
while executing
"puts $a"
% p uts $errorCode
=> NONE
7、return命令
可以使用 return 来返回调用。return 的位置可以根据各种条件和需要进行安排,而且一个过程中可以包含多条 return 命令,但当遇到第一个可执行的 return 命令时就返回。
语法:retrun ?-code cd? ?-errorinfo info? ?-errorcode errc? str
-code 的选项值是 ok、error、return、break、continue,也可以是一个整数。默认为 ok
-code error 选项使 return 命令功能和 error 非常相似。此时-errorcode 选项设置全局变量
errorCode,而-errorinfo 选项为 errorInfo 提供辅助信息。
proc stub {} {
return -code error
function stub"
}
stub
=>Some error in
puts $errorInfo
=> Function stub
invoked from
"stub"
set errorCode
=> 30
8、exit命令
exit 命令用来终止脚本的执行。exit 会终止并退出整个运行脚本的进程(退出 Tcl shell),
使用时要小心。如果在退时提供了一个整数数值,则它代表退出状态。