无法运行期望脚本:无效的命令名称“是/否”

时间:2018-11-10 15:47:19

标签: expect

我有这个期望脚本,需要执行其他一些Shell脚本来接受许可协议

#!/usr/bin/expect
spawn ./xxx.sh
expect -ex "--More--"
send -- " "
expect "Do you agree with this license ?[Yes/No]"
send "Y\r"

但是当我运行它时,会出现此错误

invalid command name "Yes/No"
while executing
"Yes/No"
invoked from within
"expect "Do you agree with this license ?[Yes/No]""
(file "./xxx.sh" line 5)

我不知道我在做什么错

1 个答案:

答案 0 :(得分:2)

expect是Tcl语言的扩展。在Tcl中,command substitution使用方括号。像bash shell一样,命令替换发生在双引号引起来的字符串中。

要防止您的代码尝试执行private static readonly Regex numTextSplitRegex = new Regex(@"(?<=\D)(?=\d)|(?<=\d)(?=\D)", RegexOptions.Compiled); public int Compare(string x, string y) { x = x ?? ""; y = y ?? ""; string[] xParts = numTextSplitRegex.Split(x); string[] yParts = numTextSplitRegex.Split(y); bool firstXIsNumber = xParts[0].Length > 0 && Char.IsDigit(xParts[0][0]); bool firstYIsNumber = yParts[0].Length > 0 && Char.IsDigit(yParts[0][0]); if (firstXIsNumber != firstYIsNumber) { return x.CompareTo(y); } for (int i = 0; i < Math.Min(xParts.Length, yParts.Length); i++) { int result; if (firstXIsNumber == (i % 2 == 0)) { // Compare numbers. long a = Int64.Parse(xParts[i]); long b = Int64.Parse(yParts[i]); result = a.CompareTo(b); } else { // Compare texts. result = xParts[i].CompareTo(yParts[i]); } if (result != 0) { return result; } } return xParts.Length.CompareTo(yParts.Length); } 命令,请执行以下操作:

  1. 使用不同的引号:Tcl使用花括号作为非插入引号:

    Yes/No
  2. 退出方括号以防止命令替换:

    expect {Do you agree with this license ?[Yes/No]}