2015年(19)
分类: 服务器与存储
2015-11-10 10:49:26
TCL中的控制结构是通过使用命令来实现的,命令中有循环命令:while,foreach和for。还有条件命令:if和switch。错误处理命令:catch。还有一些控制微调结构的命令,如:break,continue,return和error。
一.if then else
这个命令的语法为
if espression then body1 else body2
看这个程序:
[ppcorn@localhost ppcorn]$ cat iftest1.tcl
#!/usr/bin/tclsh
#####################################################
# This program used to test if then eles
# The number input by keyboard will be divide by 10
####################################################
puts -nonewline "Please input a number: "
flush stdout;
set x [gets stdin]
if {$x==0} then {
puts stderr "Divide by zero"
} else {
set slope [expr 10/$x]
puts $slope
}
[ppcorn@localhost ppcorn]$ ./iftest1.tcl
Please input a number: 0
Divide by zero
[ppcorn@localhost ppcorn]$ ./iftest1.tcl
Please input a number: 2
5
这个程序中,请大家注意一下读入键盘输入的方法,先执行flush stdout,然后使用get stdin来读键盘输入。
还有一个需要注意的是在程序的第一个字符为#的话,表示这行被注释。
同时,在这个结构中,then是可以省略的,也就是程序也可以是这个样子
[ppcorn@localhost ppcorn]$ cat iftest2.tcl
#!/usr/bin/tclsh
#####################################################
# This program used to test if eles
# The number input by keyboard will be divide by 10
####################################################
puts -nonewline "Please input a number: "
flush stdout;
set x [gets stdin]
if {$x==0} {
puts stderr "Divide by zero"
} else {
set slope [expr 10/$x]
puts $slope
}
除了if else外,还有if elseif else的用法,用来处理有更多可能结果的情况,如下面的这个程序
[ppcorn@localhost ppcorn]$ cat iftest3.tcl
#!/usr/bin/tclsh
#####################################################
# This program used to test if elesif
# The number input by keyboard will be compared by 0
####################################################
puts -nonewline "Please input a number: "
flush stdout;
set x [gets stdin]
if {$x<0} then {
puts "The input number $x less than 0"
} elseif {$x==0} {
puts "The input number $x equal 0"
} else {
puts "The input number $x large than 0"
}
[ppcorn@localhost ppcorn]$ ./iftest3.tcl
Please input a number: -1
The input number -1 less than 0
[ppcorn@localhost ppcorn]$ ./iftest3.tcl
Please input a number: 0
The input number 0 equal 0
[ppcorn@localhost ppcorn]$ ./iftest3.tcl
Please input a number: 1
The input number 1 large than 0
二.switch
switch命令根据表达式的值的不同来执行多个分支命令中的一个,该命令的一般形式为:
switch flags value pat1 body1 pat2 body2
对于flags来说,可以为如下值:
-exact 精确匹配,也是默认值
-glob 使用通配符的格式
-regexp 使用正则表达式匹配
-- 没有标志(或者标志结束)。当value以-开始的时候,必须用到这个。
看一个使用精确匹配的例子
[ppcorn@localhost ppcorn]$ cat switchtest1.tcl
#!/usr/bin/tclsh
#####################################################
# This program used to test switch
# The number input will be comared with 0,10,100
####################################################
puts -nonewline "Please input a number: "
flush stdout;
set x [gets stdin]
switch -exact $x {
0 {puts "The input is 0"}
10 {puts "The input is 10"}
100 {puts "The input is 100"}
default {puts "Hello"}
}
[ppcorn@localhost ppcorn]$ ./switchtest1.tcl
Please input a number: 0
The input is 0
[ppcorn@localhost ppcorn]$ ./switchtest1.tcl
Please input a number: 10
The input is 10
[ppcorn@localhost ppcorn]$ ./switchtest1.tcl
Please input a number: 100
The input is 100
[ppcorn@localhost ppcorn]$ ./switchtest1.tcl
Please input a number: 90
Hello
在上个程序中,如果读入的是0,10,100,分别执行各自内部的指令,如果都不是,则执行默认的指令,也就是default内部的。
再看一个使用通配符的程序
[ppcorn@localhost ppcorn]$ cat switchtest2.tcl
#!/usr/bin/tclsh
#####################################################
# This program used to test switch
# The word input will be comared with a,e,i,o,u
####################################################
puts -nonewline "Please input word: "
flush stdout;
set x [gets stdin]
switch -glob $x {
*a* {puts "The word has alpha a"}
*e* {puts "The word has alpha e"}
*i* {puts "The word has alpha i"}
*o* {puts "The word has alpha o"}
*u* {puts "The word has alpha u"}
default {puts "The word is error"}
}
[ppcorn@localhost ppcorn]$ ./switchtest2.tcl
Please input word: pat
The word has alpha a
[ppcorn@localhost ppcorn]$ ./switchtest2.tcl
Please input word: bed
The word has alpha e
[ppcorn@localhost ppcorn]$ ./switchtest2.tcl
Please input word: bit
The word has alpha i
[ppcorn@localhost ppcorn]$ ./switchtest2.tcl
Please input word: old
The word has alpha o
[ppcorn@localhost ppcorn]$ ./switchtest2.tcl
Please input word: push
The word has alpha u
[ppcorn@localhost ppcorn]$ ./switchtest2.tcl
Please input word: rt
The word is error
三.while
while接受两个变元,一个是测试表达式,一个命令体,如:
while booleanExpr body
看下面的程序,输入一个数字后,计算1到该数字的累加之和。
[ppcorn@localhost ppcorn]$ cat whiletest.tcl
#!/usr/bin/tclsh
#####################################################
# This program used to test while
# The program will add from 1 to the number input by keyboard
####################################################
puts -nonewline "Please input a number: "
flush stdout;
set x [gets stdin]
set j 0
set i 1
while {$i<$x} {
set j [expr $j+$i]
incr i
}
puts $j
[ppcorn@localhost ppcorn]$ ./whiletest.tcl
Please input a number: 10
45
请大家注意程序中incr i的用法,这个表示自动给i加1,类似别的语言中的i++,如果每次都要自动加2,则写为incr i 2,如果是递减的话,则是incr i -1。
四.for
for命令的格式如下
for initial test final body
将上面的while程序用for来实现
[ppcorn@localhost ppcorn]$ cat fortest.tcl
#!/usr/bin/tclsh
#####################################################
# This program used to test for
# The program will add from 1 to the number input by keyboard
####################################################
puts -nonewline "Please input a number: "
flush stdout;
set x [gets stdin]
set j 0
for {set i 0} {$i<$x} {incr i} {
set j [expr $j+$i]
}
puts $j
[ppcorn@localhost ppcorn]$ ./fortest.tcl
Please input a number: 10
45
五.foreach
foreach命令循环执行一个命令体,每次将一个或多个列表中的值赋给一个或多个变量,一般的语法为:
foreach loopVar valuelist commandBody.
关于列表的用法,将在下面讲到。
看这个程序
[ppcorn@localhost ppcorn]$ cat foreachtest.tcl
#!/usr/bin/tclsh
#####################################################
# This program used to test foreach
#####################################################
foreach value {1 3 2 11 5 4 7 6 9} {
puts $value
}
[ppcorn@localhost ppcorn]$ ./foreachtest.tcl
1
3
2
11
5
4
7
6
9
六.break,continue
在循环中,如果遇到了break,则会自动退出程序,而如果是遇到了continue,则继续执行程序。两个的命令的用法将会以后介绍