Enter 的特殊含义:expect -re "Press \[Enter\] to continue:" 不起作用

Special meaning of Enter: expect -re "Press \[Enter\] to continue:" does not work

我对expect脚本中的正则表达式处理有点不解。我生成的可执行文件将发回几条消息,其中包含 Press [Enter] to continue: 之类的文本。我认为处理应该如下:

spawn /tmp/install.run --prefix /opt --mode text

expect {
    timeout { send_user "\nFailure to initiate license view\n"; exit 1 }
    eof { send_user "\nGeneral error\n"; exit 1 }
    -re "Press \[Enter\] to continue:" { send "\r" }
}

但是,脚本拒绝识别返回的文本。它仅在使用此正则表达式时有效:"Press .Enter\] to continue:".

expect 脚本中的 Enter 关键字有什么特别之处吗?

由于您使用了 "" 引号而不是 {} 大括号,TCL 会在字符串到达​​正则表达式引擎之前处理反斜杠,生成一个没有反斜杠的字符串。这意味着 \[Enter\] 被视为字符 class [Enter].

对正则表达式使用大括号:

    -re {Press \[Enter\] to continue:} { send "\r" }