How can I assign a variable using $expect_out in TCL/EXPECT?

后端 未结 3 817
[愿得一人]
[愿得一人] 2021-02-09 02:14

If I want to match DEF_23 using the following regexp:

expect {
    -re \"DEF_\\[0-9]*\"
    set result $expect_out(1,string)
}

why

3条回答
  •  遥遥无期
    2021-02-09 02:55

    It seems that the above explication is not precise! Check this example:

    $ cat test.exp
    #!/usr/bin/expect
    
    set timeout 5
    log_user 0
    
    spawn bash
    
    send "ls -1 db*\r"
    expect {
      -re "^db.*$" {
        set bkpfile $expect_out(0,string)
      }
    }
    
    send_user "The filename is: $bkpfile\n"
    
    close
    $ ls -1 db*
    dbupgrade.log
    $ ./test.exp
    can't read "bkpfile": no such variable
        while executing
    "send_user "The filename is: $bkpfile\n""
        (file "./test.exp" line 15)
    $
    

    The test result is the same when $expect_out(1,string) or $expect_out(buffer)is used. Am I missing something or this is the expected behavior?

提交回复
热议问题