【问题标题】:Expect Script to wait for specific input for a certain amount of time期望脚本等待特定输入一段时间
【发布时间】:2018-06-22 15:57:20
【问题描述】:

我正在通过 docker 安装 Windows 应用程序,这需要一些用户输入,如 DB 设置等。我正在为这些用户输入使用期望脚本来自动化我们的安装。

但在最后一步中,我的安装需要 15-20 分钟,因为应用程序需要安装 DB 架构和其他必需元素,然后给出最终输入以按 Enter 键退出安装过程。

我该如何处理它,目前我只是让我的期望脚本等待,但是无论如何我可以处理这个并让期望脚本等待那个特定的输入“字符串匹配”?

这是我的期望脚本的样子

    #!/bin/bash

expect -c '
  spawn sh ./setupapp.sh
  expect "PRESS <ENTER> TO CONTINUE:"
  send "\r"
  expect "PRESS <ENTER> TO CONTINUE:"
  send "\r"
  expect "PRESS <ENTER> TO CONTINUE:"
  send "\r"
  expect "PRESS <ENTER> TO CONTINUE:"
  send "\r"
  expect "PRESS <ENTER> TO CONTINUE:"
  send "\r"
expect "PRESS <ENTER> TO CONTINUE:"
  send "\r"
  expect "Waiting here:"
  send "\r"
  expect "Waiting here:"
  send "\r"
  expect "Waiting here:"
  send "\r"
  expect "Waiting here:"
  send "\r"
  expect "Waiting here:"
  send "\r"
  expect "Waiting here:"
  send "\r"
 expect "PRESS <ENTER> TO Exit Installation:"
 send "\r"
  expect eof
'

我在这里使用 Waiting 让它等待 10 秒,有什么方法可以自动化并等待最后一个字符串按 Enter 退出安装。

谢谢!

【问题讨论】:

  • 当然,我会看看它。谢谢你的建议。

标签: linux sh expect


【解决方案1】:

看起来您想要这样的事情:这是预期某件事会发生多次的最灵活的方式,我们不必关心它确切地发生了多少次。 p>

expect -c '
  set timeout 1200   ;# 20 minutes
  spawn sh ./setupapp.sh
  expect {
    "PRESS <ENTER> TO CONTINUE:" {
      send "\r"
      exp_continue
    }
    "Waiting here:" {
      send "\r"
      exp_continue
    }
    timeout {
      error "nothing happened after $timeout seconds" 
    }
    "PRESS <ENTER> TO Exit Installation:" {
      send "\r"
    }
  }
  expect eof
'

那个 expect 命令等待四件事之一发生。对于前 2 个,按 Enter 键,然后继续等待另一个事件。我添加了“超时”事件,以防你想在那里做一些特别的事情。

“按回车退出”块调用“exp_continue”。在它发送回车后,封闭的expect命令结束,然后我们等待eof。

【讨论】:

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