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
    ---1---
    I inside first loop: 0
    I inside first loop: 1
    I inside first loop: 2
    I inside first loop: 3
    I inside first loop: 4
    I inside first loop: 5
    I inside first loop: 6
    I inside first loop: 7
    I inside first loop: 8
    I inside first loop: 9
    ---2---
    ---3---
    Start
    I inside third loop: 0
    I after incr: 1
    I inside third loop: 1
    I after incr: 2
    I inside third loop: 2
    I after incr: 3
    I inside third loop: 3
    I after incr: 4
    I inside third loop: 4
    I after incr: 5
    I inside third loop: 5
    I after incr: 6
    I inside third loop: 6
    I after incr: 7
    I inside third loop: 7
    I after incr: 8
    I inside third loop: 8
    I after incr: 9
    I inside third loop: 9
    I after incr: 10
    ---4---
    1
    ---5---
    2


===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

==当前用户是root,我想用 su - oracle,然后在oracle下使用 expect -c 命令,不想用脚本,想用一行命令实现
su - oracle -c "expect -c 'spawn sqlplus / as sysdba; expect \"SQL>\"; send \"alter user sys identified by 123456 account unlock;\r\"; send \"exit;\r\"'"

你可能感兴趣的:(expect语法基础: while、for 循环、if 语句的用法示例)