本篇內容介紹了“expect的基礎用法”的有關(guān)知識,在實(shí)際案例的操作過(guò)程中,不少人都會(huì )遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學(xué)有所成!
以下是一個(gè)可以使用的腳本 。在這種交互式的應用中,經(jīng)常需要用到休眠函數,這樣可以對輸出的界面顯示更加友好,而且也可能盡量多的減少 錯誤的出現
關(guān)于轉義字符,網(wǎng)上存在一部分,本例中用到來(lái)-號需要轉義,轉義是用兩個(gè)\來(lái)轉義。
\ 需轉義為 \\\
} 需轉義為 \}
[ 需轉義為 \[
$ 需轉義為 \\\$
` 需轉義為 \`
" 需轉義為 \\\"
expect遵循的是tcl語(yǔ)法。這是一種標準。有時(shí)間可以參考學(xué)習。
#!/usr/bin/expect -f
set timeout -1#永不超時(shí)
set deviceType [lindex $argv 0]
set writesize [lindex $argv 1] #設置輸入變量
set i 0 #設置變量
log_file test.log #記錄log。
#log_file -noappend rtime_md_test_${stationNum}_[clock format $currenttime -format %m%d%H].log
spawn /home/redhat/Downloads/Mitsubishi/Latest/Driver/Sample/md_test
expect {
"menu No" {exp_send "1\r" ;exp_continue}
"ChanelNo" {exp_send "151\r"}
}
expect {
"mode = " {exp_send "\\-1\r"} #這里比較坑爹,害我查了很久很久,最后試出來(lái)的
}
while {$i<10} { #循環(huán)語(yǔ)法使用
#寫(xiě)入數據部分
expect {
"menu No" {exp_send "11\r"}
}
#exec sleep 1
expect {
"StationNo" {exp_send "255\r"}
}
exec sleep 0.01 #休眠時(shí)間可以是小數
expect {
"NetworkNo" {exp_send "1\r"}
}
exec sleep 0.01
expect {
"DeviceType" {exp_send "$deviceType\r"}
}
exec sleep 0.01
expect "DeviceNo.(or ChannelNo.)" {exp_send "0\r"}
exec sleep 0.01
expect "WriteSize" {exp_send "$writesize\r"}
exec sleep 0.01
expect "WriteDataType" {exp_send "2\r"}
#讀取數據部分
exec sleep 0.1
expect "menu No." {exp_send "12\r"}
exec sleep 0.01
expect {
"StationNo" {exp_send "255\r"}
}
exec sleep 0.01
expect {
"NetworkNo" {exp_send "1\r"}
}
exec sleep 0.01
expect {
"DeviceType" {exp_send "$deviceType\r"}
}
exec sleep 0.01
expect "DeviceNo.(or ChannelNo.)" {exp_send "0\r"}
exec sleep 0.01
expect "ReadSize" {exp_send "$writesize\r"}
incr i
}
expect "menu No" {exp_send "0\r"}
expect eof
expect語(yǔ)法基礎: while、for 循環(huán)、if 語(yǔ)句的用法示例
==兩種for循環(huán)的寫(xiě)法
for {set i 0} {$i<=10} {incr i} {#i默認增量是1,即等價(jià)incr i 1。注意這個(gè)反括號一定要寫(xiě)在這行行末:args: should be "for start test next command"
............
.............}
Q:能不能改為i為我指定的幾個(gè)數就好。比如我指定i為 3 5 6 7 9這幾個(gè)數? 謝謝。
foreach i { 1 3 5 7 9 } {
puts "$i"
}
注:expect 用的是tcl語(yǔ)法,不是shell語(yǔ)法,或者用switch
==for/while循環(huán)寫(xiě)法
[15:33:05-Bob@hzling08:~/test/tcl]-(1109)No.108->$ cat tclfor.test
#!/usr/bin/expect --
# http://bbs.chinaunix.net/thread-2301733-1-1.html
# for Bob testing
#
puts "---1---"
for {set i 0} {$i < 10} {incr i} {
puts "I inside first loop: $i"
}
puts "---2---"
for {set i 3} {$i < 2} {incr i} {
puts "I inside second loop: $i"
}
puts "---3---"
puts "Start"
set i 0
while {$i < 10} {
puts "I inside third loop: $i"
incr i
puts "I after incr: $i"
}
set i 0
incr i
puts "---4---"
puts "$i"
# This is equivalent to:
set i [expr {$i + 1}] #expect里的加減法
puts "---5---"
puts "$i"
運行:
[15:33:09-Bob@hzling08:~/test/tcl]-(1110)No.109->$ ./tclfor.test
===if的寫(xiě)法
if { $sync_flag == "true" } {
puts "Sync start at [clock format [clock seconds]]"
catch {eval exec ${TOOL_HOME}/bin/${sync_cmd} ${sync_parm} } output
puts $output
if { $output eq "SYNC complete!" } {
puts "SYNC complete!"
} else {
puts "SYNC error!"
exit 1
}
puts "Sync end at [clock format [clock seconds]]"
}
===ping的例子
set p_loop 5
while { $p_loop } {
send_user "\nStpe 1 Ping to server..."
set timeout 60
send "ping 10.1.1.1 -c5\r"
expect {
"64 bytes" {
send_user "ok"
set p_loop 0
}
timeout {
set p_loop [expr $p_loop-1] #expect里的加減法
send_user "failed.\n"
}
eof {
send_user "ping 10.1.1.1 -c5 FAIL\n"
exit 1
}
}
===expect讀取文件的例子
#!/usr/bin/expect --
# http://scmbob.org/counting_file_lines.html
#open a file
set fd [open "/home/xiabao/myfile.txt" r]
set number 0
# read each line
while { [gets $fd line] >= 0 } { incr number }
puts "Number of lines: $number"
close $fd
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站