TCL语法简单介绍

974阅读 0评论2008-08-12 hb_li_520
分类:系统运维

1.变数和变数的替换
variable.tcl 

set name "able"
puts "my name is $name"

执行方法:ns variable.tcl

执行结果: my name is able

set month 8
set day 13
set year 2008
set date "$month:$day:$year"
puts $date

执行结果: 8:13:2008

set foo "puts hello!"
eval $foo

执行结果:hello!
 
2.表达式
 

set value [expr 10+50]
puts $value

执行结果60

3.流程控制

 

set my_planet "earth"
if {$my_planet == "earth"} {
puts "I feel right at home."
} elseif {$my_planet == "venus"} {
   puts "This is not my home."
} else {
   puts "I am neither from Earth, nor from Venus."
}

set temp 95
if {$temp < 80} {
    puts "It's a little chilly."
} else {
    puts "Warm enough for me."
}

执行结果: I feel right at home.

           Warm enouth for me.

4.程序
 

proc sum_proc {a b} {
return [expr $a + $b]
}

proc magnitude {num} {
if {$num > 0} {
return $num
}

set num [expr $num * (-1)]
return $num
}

set num1 12
set num2 14
set sum [sum_proc $num1 $num2]

puts "The sum is $sum"
puts "The magnitude of 3 is [magnitude 3]"
puts "The magnitude of -2 is [magnitude -2]"

执行结果:The sum is 26

          The maguitude of 3 is 3

          The magnitude of -2 of 2

上一篇:丢包后的多媒体测试
下一篇:VLAN 详解