ruby - Ruby - 在 Windows 环境中调用命令并与 shell 交互
问题描述
我正在命令行中从 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|
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
推荐阅读
- javascript - 基于 HTML 表格的游戏中的 Javascript 函数
- django - 同时处理多个 Pcap 文件 - Django
- ios - “SingleEvent<_>”中的成员“成功”产生“SingleEvent”类型的结果
' - css - 视口缩放适用于图像宽度但不适用于高度
- postgresql - 如何通过跳过索引来运行 pgloader?
- c# - C# 将文件夹中的最后一个文件复制到另一个生成的文件夹中
- python - 通过设置或 Python LibVLC API 对 VLC 网络流中的自动重新连接失败进行故障排除
- html - div标签中的背景图像大小 - 全屏 - 内联css
- android - 如何检查应用程序是否使用 jni 安装?
- ajax - 阿贾克斯和 Django