Ruby-在Windows环境中调用命令并与Shell进行交互 [英] Ruby - Calling commands and interact with shell in Windows environment

查看:70
本文介绍了Ruby-在Windows环境中调用命令并与Shell进行交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am running an .exe file in the command line from a Ruby script that asks the user for a Yes/No response. I would like to know how the user can interact with it in Windows environment.

我尝试了所有可能的选项: system ,反引号,%x(),Open3,Open4 ...都不起作用。

I have tried all the possible options: system, backticks, %x(), Open3, Open4... and none of them work.

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

Some posts ([1], [2]) resolve the issue using PTY, but as to my knowledge there is no implementation of the PTY module in Windows. Any other idea?

推荐答案

似乎在Windows下也可以使用

Seems this is working under Windows too

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

这篇关于Ruby-在Windows环境中调用命令并与Shell进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆