以下是一個可以使用的腳本 。在這種交互式的應用中,經常需要用到休眠函數,這樣可以對輸出的界面顯示更加友好,而且也可能盡量多的減少 錯誤的出現
關于轉義字符,網上存在一部分,本例中用到來-號需要轉義,轉義是用兩個\來轉義。
\ 需轉義為 \\\
} 需轉義為 \}
[ 需轉義為 \[
$ 需轉義為 \\\$
` 需轉義為 \`
" 需轉義為 \\\"
可以用log_user 0或1實現輸出屏蔽和顯示 log_file test.log #記錄log。追加 |
expect遵循的是tcl語法。這是一種標準。有時間可以參考學習。
#!/usr/bin/expect -f
set timeout -1#永不超時
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"} #這里比較坑爹,害我查了很久很久,最后試出來的
}
while {$i<10} { #循環語法使用
#寫入數據部分
expect {
"menu No" {exp_send "11\r"}
}
#exec sleep 1
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 "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語法基礎: while、for 循環、if 語句的用法示例
==兩種for循環的寫法
for {set i 0} {$i<=10} {incr i} {#i默認增量是1,即等價incr i 1。注意這個反括號一定要寫在這行行末:args: should be "for start test next command"
............
.............}
Q:能不能改為i為我指定的幾個數就好。比如我指定i為 3 5 6 7 9這幾個數? 謝謝。
foreach i { 1 3 5 7 9 } {
puts "$i"
}注:expect 用的是tcl語法,不是shell語法,或者用switch
==for/while循環寫法
[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的寫法
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
“expect的基礎用法”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注本站網站,小編將為大家輸出更多高質量的實用文章!
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
c語言中正確的字符常量是用一對單引號將一個字符括起表示合法的字符常量。例如‘a’。數值包括整型、浮點型。整型可用十進制,八進制,十六進制。八進制前面要加0,后面...
2022年天津專場考試原定于3月19日舉行,受疫情影響確定延期,但目前延期后的考試時間推遲。 符合報名條件的考生,須在規定時間登錄招考資訊網(www.zha...
:喜歡聽,樂意看。指很受歡迎?!巴卣官Y料”喜聞樂見:[ xǐ wén lè jiàn ]詳細解釋1. 【解釋】:喜歡聽,樂意看。指很受歡迎。2. 【示例】:這是...
近期,黃金價格快速上漲,并創出18 個月新高。2 月24 日晚,COMEX黃金期貨價格最高達到1976.5 美元/盎司,創下自2020 年9 月16 日以來新高。與此同時,倫敦金現也表現出了相同趨勢,最高價達到了1974.4美元/盎司,創出18 個月來的最高價。機構認為:國際地緣局勢進一步緊張是近期黃金價格上行的主要原因。因外圍突發消息,黃金價格大幅上行,倫敦金現價格當日最高上漲4.1%,創下近一...
中信銀行電子賬戶具體含義。中信銀行電子賬戶是指用戶通過電子渠道實名開立的,可以購買投資理財產品等指定金融產品的賬戶,屬于非面對面開立的二類銀行賬戶或三類銀行賬戶。要在中信銀行,開立電子賬戶,您必須年滿16歲,在身份證的正面和背面提供真實、完整和準確的信息,并核實手機號碼必須與綁定到該賬戶的手機號碼一致。中信銀行電子賬戶可以為用戶提供金融產品和服務,但不會向用戶發送紙質票據,用戶只能通過指定的電子渠...
網下申購和網上申購在股市中,打新一項風險低但是回報高的投資,新股申購分為兩種方式,分別是網下申購與網上申購,那么網下申購是什么意思?網下申購和網上申購的區別是什么呢?"網下申購"就是不通過證券交易所的網上交易系統進行的申購,網下申購此申購一般對大資金大機構進行。網下申購又叫網下配售。網下申購是指不在股票交易網內向一些機構投資者配售股票,證監會網站2012年4月28日晚間公布的消...