【问题标题】:"invalid command name" in expect script期望脚本中的“无效命令名称”
【发布时间】:2016-07-07 20:29:18
【问题描述】:

我有以下用于在串行端口上监听的代码:

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; }
}

为什么会给出一个

无效的命令名称“

在程序中超过 5 秒超时后出错?嵌套的期望可以吗?

【问题讨论】:

    标签: tcl expect


    【解决方案1】:

    问题是这样的:

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

    换行符在 Tcl 脚本中很重要!当您单独使用expect 时,它只是等待超时(并处理您设置的任何背景;在这种情况下没有)。下一行,你在等待什么,被解释为一个命令,它自己有一个非常奇怪的名字(包括换行符、空格等),这根本不是你想要的。

    你真正想做的是:

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

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

    【讨论】:

      猜你喜欢
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-11
      • 1970-01-01
      • 2017-06-11
      • 2016-09-06
      相关资源
      最近更新 更多