首页 > 解决方案 > 期望不匹配

问题描述

我有一个可以运行的期望脚本;它包含以下几行:

expect "\[Y\]\> "
send "n\r"

我正在与之交互的操作系统发生了变化,我现在有一个混合的盒子环境。有些现在有“[Y]>”,有些有“[1]>”,我正在尝试处理。

我尝试将代码更改为:

expect {
        "\[Y\]\> " { send "n\r";exp_continue }
        "\[1\]\> " { send "2\r";exp_continue }
}

但是,在调试中运行我看到:

"Choose the password option:
1. Mask passwords 
2. Plain passphrases
[1]>
expect: does "s...ses\r\n[1]> " (spawn_id exp5) match glob pattern "[Y]> "? no 
"[1]> "? no
expect: timed out"

我不明白为什么修改后的代码不起作用,对于“[Y]>”或“[1]>”,当“[Y]>”在引入“else”语句之前被匹配时。

标签: tclexpect

解决方案


问题在于转义方括号,这是Tcl.

# Using braces for pattern
expect {
    {\[Y]>} {puts Y}
    {\[1]>} { puts 1}
}
# or 
# Using double quotes
expect {
    "\\\[Y]>" {puts Y}
    "\\\[1]>" { puts 1}
}

我们不必转义右括号]和符号>


推荐阅读