【问题标题】:Ruby - Calling commands and interact with shell in Windows environmentRuby - 在 Windows 环境中调用命令并与 shell 交互
【发布时间】:2019-04-17 10:56:51
【问题描述】:

我正在命令行中从一个 Ruby 脚本运行一个 .exe 文件,该脚本询问用户是/否响应。我想知道用户如何在 Windows 环境中与之交互。

我已经尝试了所有可能的选项:system、反引号、%x()、Open3、Open4……但它们都不起作用。

一些帖子([1][2])使用 PTY 解决了该问题,但据我所知,Windows 中没有 PTY 模块的实现。还有什么想法吗?

【问题讨论】:

  • 我无法在 Windows 下测试,但可能有帮助:pipe = IO.popen('your.exe', 'w+', :err => [:child, :out]) 然后pipe.each_line do |line| 并测试问题,然后pipe.puts('Yes') 最后pipe.close
  • @lojza 行得通!之前没试过IO,谢谢!

标签: ruby windows shell command-line interop


【解决方案1】:

看来这在 Windows 下也可以工作

pipe = IO.popen('your.exe', 'w+', :err => [:child, :out])
@pipe.each_line do |line|
  if /pattern matching question/ =~ line
    break
  end
end
pipe.puts('Yes')
# another test can be here
pipe.close

明智地与https://ruby-doc.com/stdlib/libdoc/timeout/rdoc/Timeout.html一起使用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 2012-05-25
    • 1970-01-01
    • 2017-11-24
    • 2016-02-17
    • 2016-04-06
    相关资源
    最近更新 更多