Ruby - Calling commands and interact with shell in Windows environment

邮差的信 提交于 2020-01-25 10:02:48

问题


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.

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

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?


回答1:


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

Wise to use with https://ruby-doc.com/stdlib/libdoc/timeout/rdoc/Timeout.html



来源:https://stackoverflow.com/questions/55725954/ruby-calling-commands-and-interact-with-shell-in-windows-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!