期待:成功的正则表达式匹配"。+"没有设置$ expect_out(0,string)

时间:2016-01-18 15:56:01

标签: regex expect

在以下期望脚本中,您可以看到我正在尝试匹配正则表达式"。*" 。该匹配成功,但$ expect(0,字符串)仍未设置。我做错了什么?

首先,脚本(在名为/ usr / local / bin / py的文件中)......

#!/usr/bin/expect -d

log_user 1
exp_internal 1
match_max 100000

spawn /usr/bin/python3

set prompt       "(>>>|\\.\\.\\.) "
set anythingElse ".+"

expect {
  -re $prompt {
    send_user -- $expect_out(0, string)
    expect_user -re "((>>>|\\.\\.\\.) )?(.*)\n"
    send -- $expect_out(3, string)
    send -- "\n"
    exp_continue
  }
  -re $anythingElse {
    set x $expect_out(0, string)
    send_user -- $x
    exp_continue
  }
  eof {
    exit 0
  }
}

现在,这是输出。请注意,失败发生在"设置x ..."行:

expect version 5.45
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = /usr/local/bin/py  
set argc 0
set argv0 "/usr/local/bin/py"
set argv ""
executing commands from command file /usr/local/bin/py
spawn /usr/bin/python3
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {1277}
Gate keeper glob pattern for '(>>>|\.\.\.) ' is ''. Not usable, disabling the performance booster.
Gate keeper glob pattern for '.+' is ''. Not usable, disabling the performance booster.

expect: does "" (spawn_id exp6) match regular expression "(>>>|\.\.\.) "? (No Gate, RE only) gate=yes re=no
".+"? (No Gate, RE only) gate=yes re=no
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.

expect: does "Python 3.4.3 (default, Oct 14 2015, 20:28:29) \r\n[GCC 4.8.4] on linux\r\nType "help", "copyright", "credits" or "license" for more information.\r\n" (spawn_id exp6) match regular expression "(>>>|\.\.\.) "? (No Gate, RE only) gate=yes re=no
".+"? (No Gate, RE only) gate=yes re=yes
expect: set expect_out(0,string) "Python 3.4.3 (default, Oct 14 2015, 20:28:29) \r\n[GCC 4.8.4] on linux\r\nType "help", "copyright", "credits" or "license" for more information.\r\n"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Python 3.4.3 (default, Oct 14 2015, 20:28:29) \r\n[GCC 4.8.4] on linux\r\nType "help", "copyright", "credits" or "license" for more information.\r\n"
can't read "expect_out(0, string)": no such element in array
    while executing
"set x $expect_out(0, string)"
    invoked from within
"expect {
  timeout {
    exp_continue
  }
  -re $prompt {
    send_user -- $expect_out(0, string)
    send_user -- "!!!\r\n"
    expect_user -re "((>>..."
    (file "/usr/local/bin/py" line 14)

提前致谢。

1 个答案:

答案 0 :(得分:5)

我想出了答案。逗号和expect_out()的第二个参数之间不能有空格。

换句话说,这是错误的: $ expect_out(0, [space] 字符串)

...这是正确的: $ expect_out(0,string)