"invalid command name" 在预期脚本中

"invalid command name" in expect script

我有以下串口监听代码:

set timeout -1
log_user 0

set port [lindex $argv 0]

spawn /usr/bin/cu -l $port

proc receive { str } {
  set timeout 5
  expect
  {
    timeout { send_user "\nDone\n"; }
  }
  set timeout -1
}

expect {
  "XXXXXX\r" { receive $expect_out(0,string); exp_continue; }
}

为什么这会产生

invalid command name "

程序超时5秒后出错?嵌套期望是否可以?

问题是这样的:

  expect
  {
    timeout { send_user "\nDone\n"; }
  }

换行符在 Tcl 脚本中很重要!当您单独使用 expect 时,它只是等待超时(并处理任何需要您设置的后台;在本例中为 none)。下一行,以及您正在等待的内容,被解释为一个具有非常奇怪名称(包括换行符、空格等)的命令,这根本不是您想要的。

你真正想做的是:

  expect {
    timeout { send_user "\nDone\n"; }
  }

通过将大括号与 expect 放在同一行,您将获得(大概)预期的行为。