TCL(踢叩)用实例学习之expect

yum -y install expect
[root@localhost ~]#vim test.exp

#!/usr/bin/expect
set foo "gaby"
set month 2
set day 3
set year 2012
set date "$month:$day:$year"
puts $date
puts "hi, my name is $foo"                                                
[root@localhost ~]# expect test.exp    
2:3:2012
hi, my name is gaby
[root@localhost ~]# vim test1.exp

#!/usr/bin/expect
set foo "puts gaby"
eval $foo
[root@localhost ~]# expect test1.exp
gaby
[root@localhost ~]#
##expr用于数学表达式,关系表达式(两数比较,true 则输出1,false 输出0)##
[root@localhost ~]# vim test2.exp
#!/usr/bin/expect
set my_height 6.0
puts "[expr 0 == 1]"
puts "[expr 1 == 1]"
puts "[expr 4 + 5]"
puts "[expr sin(2)]"
puts "I am [expr 100 / 5] years old, and my I.Q. is [expr 100 - 25]"
puts "If I was 2 inches taller, I would be [expr $my_height + (2.0 / 12.0)] feet tall"
[root@localhost ~]# expect test2.exp
0
1
9
0.909297426826
I am 20 years old, and my I.Q. is 75
If I was 2 inches taller, I would be 6.16666666667 feet tall
[root@localhost ~]#
##判断流转(if-else; switch)和循环控制(while; for; foreach)##
[root@localhost ~]# vim test3.exp
#!/usr/bin/expect
set gaby [lindex $argv 0]
if { $gaby > 525    } {
puts "You can on the key universities"
} elseif { $gaby >425 && $gaby < 525 } {
puts "You can generally undergraduate university"
} else {
puts "You can on the junior college"
}
[root@localhost ~]# expect test3.exp 526
You can on the key universities
[root@localhost ~]# expect test3.exp 450
You can generally undergraduate university
[root@localhost ~]# expect test3.exp 300
You can on the junior college

[root@localhost ~]# vim test4.exp

#!/usr/bin/expect
set num_legs [lindex $argv 0]
switch $num_legs {
2 {puts "It could be a human."}
4 {puts "It could be a cow."}
6 {puts "It could be an ant."}
8 {puts "It could be a spider."}
default
{if {$num_legs > 100} {puts "This is a monster"
} else {
puts "This is a magical animal"
}}
}
[root@localhost ~]# expect test4.exp    2
It could be a human.
[root@localhost ~]# expect test4.exp    4
It could be a cow.
[root@localhost ~]# expect test4.exp    6
It could be an ant.
[root@localhost ~]# expect test4.exp    8
It could be a spider.
[root@localhost ~]# expect test4.exp    10
This is a magical animal
[root@localhost ~]# expect test4.exp    
This is a magical animal
[root@localhost ~]# expect test4.exp    200
This is a monster
[root@localhost ~]# vim test5.exp

#!/usr/bin/expect
set n 0
for {set i 0} {$i <= 100} {incr i 1} {
set n [expr $n + $i]
}
puts $n
[root@localhost ~]# expect test5.exp    
5050
[root@localhost ~]# vim    test51.exp
#!/usr/bin/expect
set num [lindex $argv 0]
set n 0
for {set i 0} {$i <= $num} {incr i 2} {
set n [expr $n + $i]
}
puts $n
[root@localhost ~]# expect test51.exp 100
2550
[root@localhost ~]# vim test6.exp
#!/usr/bin/expect
foreach vowel {a e i o u} {
puts "$vowel is a vowel"
}
[root@localhost ~]# expect test6.exp 
a is a vowel
e is a vowel
i is a vowel
o is a vowel
u is a vowel
[root@localhost ~]#
#####proc 定义TCL函数##
[root@localhost ~]# vim test7.exp
#!/usr/bin/expect
set num1 [lindex $argv 0]
set num2 [lindex $argv 1]
proc sum_proc {a b} {
return [expr $a + $b]
}
set sum [sum_proc $num1 $num2]
puts "The sum is $sum"
[root@localhost ~]# expect test7.exp     10 20
The sum is 30
[root@localhost ~]# expect test7.exp     10 -5
The sum is 5
[root@localhost ~]# vim test71.exp
#!/usr/bin/expect
set num1 [lindex $argv 0]
set num2 [lindex $argv 1]
set leng1 [string length $num1]
set leng2 [string length $num2]
if {$leng1 == 0} {
puts "Please enter the two numbers"
exit 10
} elseif {$leng2 == 0} {
puts "Please enter the two numbers"
exit 10
} else {
proc sum_proc {a b} {
return [expr $a + $b]
}
set sum [sum_proc $num1 $num2]
puts "The sum is $sum"
}
[root@localhost ~]# expect test71.exp
Please enter the two numbers
[root@localhost ~]# expect test71.exp 10    
Please enter the two numbers
[root@localhost ~]# expect test71.exp    10 10
The sum is 20
[root@localhost ~]#
###tcl数组,list特殊数组,插入操作linsert,lappend,将元素添加到列表末尾###
[root@localhost ~]# vim test8.exp
#!/usr/bin/expect
set simple_list "John Joe Mary Susan"
set simple_list2 "Mike Sam Heather Jennifer"
set compound_list [list $simple_list $simple_list2]
puts [lindex $simple_list 0]
puts [lindex $simple_list 2]
puts $compound_list
puts [llength $compound_list]
set mylist "Mercury Venus Mars"
puts $mylist
puts [linsert $mylist 2 Earth]
puts $mylist
lappend mylist Jupiter
puts $mylist
append mylist Jupiter
puts $mylist
[root@localhost ~]# expect test8.exp    
John
Mary
{John Joe Mary Susan} {Mike Sam Heather Jennifer}
2
Mercury Venus Mars
Mercury Venus Earth Mars
Mercury Venus Mars
Mercury Venus Mars Jupiter
Mercury Venus Mars JupiterJupiter
#######string###
[root@localhost ~]# vim    test9.exp    

#!/usr/bin/expect
set str "This is a string"
puts "The string is: $str"
puts "The length of the string is: [string length $str]"
puts "The character at index 3 is: [string index $str 3]"
puts "The characters from index 4 through 8 are: [string range $str 4 8]
"
puts "The index of the first occurrence of letter \"i\" is: [string first i $str]"
[root@localhost ~]# expect test9.exp    
The string is: This is a string
The length of the string is: 16
The character at index 3 is: s
The characters from index 4 through 8 are:    is a

The index of the first occurrence of letter "i" is: 2
[root@localhost ~]#

你可能感兴趣的:(expect,Tcl)