期望脚本无法正常运行并发送状态被跳过

时间:2017-07-20 11:22:02

标签: expect

我已经编写了一个脚本来执行服务器上的设置,但是执行send语句时会被跳过而其他人会被执行。

以下是我的剧本:

enter code here[code written][1]




====================ERROR m getting=============================
Enter the name of the new namespace you want to create [coe_ns] :
Enter the name of the clearinghouse you want to create [coe_ch] : invalid command name "C"
    while executing
"C"
    invoked from within
"expect -re "* Option to execute (Continue/Restart/Quit) [C]: $""
    (file "./TNS_Server_Setup.sh" line 21)

我怀疑发送命令之前没有执行,我也正确地传递“ENTER”。

1 个答案:

答案 0 :(得分:0)

期望使用方括号作为命令替换语法(与shell使用反引号或$(...)的方式相同。所以当你写

expect -re "* Option to execute (Continue/Restart/Quit) [C]: $"

期望看到[C]并尝试调用命令C

此外,该独立*会抛出一个"量词操作数无效"错误,因为它是正则表达式的特殊字符。您需要将其转义或从表达式中删除它。

使用不同的引用机制来禁止命令替换(例如在你使用单引号的shell中)

expect -re {Option to execute (Continue/Restart/Quit) [C]: $}

您没有在问题中包含您的代码,因此我们无法看到您的发送命令,但只需点击输入",您就会send "\r"

更新

如果你需要匹配多个东西,expect命令可以这样做:给它一个pattern action对的列表。期望会匹配任何一个。

expect {
    -re {Option to execute (Continue/Restart/Quit) [C]: $} {
        send "\r"
        exp_continue
    }
    -re {enter the datatype $} {
        send "array\r"
        exp_continue
    }
    "some pattern that denotes we're done"
}