expect的基礎(chǔ)用法

本篇內(nèi)容介紹了“expect的基礎(chǔ)用法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

本篇內(nèi)容介紹了“expect的基礎(chǔ)用法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

固始網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,固始網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為固始近千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的固始做網(wǎng)站的公司定做!

以下是一個(gè)可以使用的腳本 。在這種交互式的應(yīng)用中,經(jīng)常需要用到休眠函數(shù),這樣可以對(duì)輸出的界面顯示更加友好,而且也可能盡量多的減少 錯(cuò)誤的出現(xiàn)

關(guān)于轉(zhuǎn)義字符,網(wǎng)上存在一部分,本例中用到來(lái)-號(hào)需要轉(zhuǎn)義,轉(zhuǎn)義是用兩個(gè)\來(lái)轉(zhuǎn)義。

\ 需轉(zhuǎn)義為 \\\

} 需轉(zhuǎn)義為 \}

[ 需轉(zhuǎn)義為 \[

$ 需轉(zhuǎn)義為 \\\$

` 需轉(zhuǎn)義為 \`

" 需轉(zhuǎn)義為 \\\"

expect遵循的是tcl語(yǔ)法。這是一種標(biāo)準(zhǔn)。有時(shí)間可以參考學(xué)習(xí)。

#!/usr/bin/expect -f

set timeout -1#永不超時(shí)

set deviceType [lindex $argv 0]

set writesize  [lindex $argv 1]  #設(shè)置輸入變量

set i 0  #設(shè)置變量

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ě)入數(shù)據(jù)部分

expect {

"menu No" {exp_send "11\r"}

}

#exec sleep 1

expect {

"StationNo" {exp_send "255\r"}

}

exec sleep 0.01         #休眠時(shí)間可以是小數(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"}

#讀取數(shù)據(jù)部分

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ǔ)法基礎(chǔ): while、for 循環(huán)、if 語(yǔ)句的用法示例
==兩種for循環(huán)的寫(xiě)法
for {set i 0} {$i<=10} {incr i} {#i默認(rèn)增量是1,即等價(jià)incr i 1。注意這個(gè)反括號(hào)一定要寫(xiě)在這行行末:args: should be "for start test next command"
............

.............}
Q:能不能改為i為我指定的幾個(gè)數(shù)就好。比如我指定i為 3 5 6 7 9這幾個(gè)數(shù)? 謝謝。
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"

運(yùn)行:

    [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

本文題目:expect的基礎(chǔ)用法
本文路徑:http://muchs.cn/article38/iopp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈標(biāo)簽優(yōu)化、自適應(yīng)網(wǎng)站響應(yīng)式網(wǎng)站服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷(xiāo)型網(wǎng)站建設(shè)