【问题标题】:gets not waiting for user input in expect script在期望脚本中不等待用户输入
【发布时间】:2018-08-19 15:54:27
【问题描述】:

当我尝试运行以下期望脚本时,它只是完成运行而不是等待用户输入。谁能告诉我我做错了什么?

#!/usr/bin/expect
puts -nonewline stdout "Enter device id:"
flush stdout
gets stdin id
puts -nonewline  stdout "Enter device name:"
flush stdout
gets stdin name

【问题讨论】:

    标签: tcl user-input expect


    【解决方案1】:

    Expect 改变了 Tcl gets 命令,使其不等待标准输入;要在等待时读取一行,您需要这样做而不是 gets stdin id

    # Read input to stdin
    expect_user -re "(.*)\n"
    set id $expect_out(1,string)
    

    【讨论】:

    • 注:我在expect文档中找到了如何从用户那里获取输入,并通过测试确认Expect改变了gets stdin的阻塞行为。我对这种变化非常感到惊讶。
    【解决方案2】:

    试试这个代码:

    expect "\\$"
    puts -nonewline "please enter a swithch user:  "
    flush stdout
    set userName [gets stdin]
    puts $userName
    expect "\\$" ;# without this line, the script would exit too fast 
                 ;# for the "echo hello" to be sent to stdout by bash
                 ;# and thus wont be recorded
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      • 2021-12-11
      • 1970-01-01
      相关资源
      最近更新 更多