日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
expect的基礎用法

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

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

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

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

關于轉(zhuǎn)義字符,網(wǎng)上存在一部分,本例中用到來-號需要轉(zhuǎn)義,轉(zhuǎn)義是用兩個\來轉(zhuǎn)義。

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

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

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

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

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

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

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} {                 #循環(huán)語法使用

#寫入數(shù)據(jù)部分

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"}

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

.............}
Q:能不能改為i為我指定的幾個數(shù)就好。比如我指定i為 3 5 6 7 9這幾個數(shù)? 謝謝。
foreach  i { 1 3 5 7 9 } {
    puts "$i"
}注:expect 用的是tcl語法,不是shell語法,或者用switch
==for/while循環(huán)寫法
    [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的基礎用法
URL標題:http://www.dlmjj.cn/article/iopp.html