Expect 学习笔记

来源:互联网 发布:jdk6 64位下载windows 编辑:程序博客网 时间:2024/04/29 07:40

一、变量

设置变量
set VARIABLE zhi
set VARIABLE [lindex $argv 0]程序后跟的第一个参数

$argc参数个数
$argv0程序名

引用变量

$VARIABLE

在子程序里调用全局变量:global i

二、数组

Expect:expect数组
赋值:

set array(n) zhi
array set arrayName $list

引用数组

$array(n)
数组大小array size arr

三、函数

proc functionName {parameter list} {

…………
}
函数可以递归调用

四、结构控制

分支结构:

类似与C中的case结构
expect {
"stringOne" {
send "dealWith1"
……
}
"stringTwo" {
send "dealWith2"
……
}
}

顺序结构:

expect ……
send ……
expect ……
send ……

循环结构:

while ($done) {

................

 

}CNC-BJ-4-3H2CC-HTTP

for循环结构

for start test next command
for {set i 0} {$i<[llength $aindex]} {incr i} {
puts $array1([lindex $aindex $i])
}

if else

if { } {

……

} else {

 

……

}

条件分支Switch:

 

switch -- $timeout_case {
0 {
...............
}
1 {
...............
}
2 {

...............

}
}

其他知识点

1.send 后面的内容不显示给用户,如果要显示给用户使用send_user 或者使用 puts stderr(显示到屏幕) or puts(可以重定向到文件) puts stdout
2.set timeout n
timeout {

……

}

timeout 单位:秒。 设置为0不等待,-1,永远等待

3. incr VARIABLE变量值加一
4. exec sleep 1等待
5. interact把控制权交给远端
6. exp_continue继续执行下面的匹配,一般在匹配密码的时候很方便
7. log_filefile可以把之后执行的内容及输出记录到文件中 默认为追加模式,如果想非追加模式在file前加参数 -noappend
8.

6.子程序窗口随父程序窗口一起改变代码

trap {
set rows [stty rows]
set cols [stty columns]
stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH

原创粉丝点击