【发布时间】:2014-04-20 22:35:37
【问题描述】:
我在 ruby 脚本中运行一个长时间运行的 shell 命令,如下所示:
Open3.popen2(command) {|i,o,t|
while line = o.gets
MyRubyProgram.read line
puts line
end
}
所以我可以在 shell 窗口中查看命令输出。
如何将 STDIN 附加到命令输入?
【问题讨论】:
我在 ruby 脚本中运行一个长时间运行的 shell 命令,如下所示:
Open3.popen2(command) {|i,o,t|
while line = o.gets
MyRubyProgram.read line
puts line
end
}
所以我可以在 shell 窗口中查看命令输出。
如何将 STDIN 附加到命令输入?
【问题讨论】:
你需要:
popen3 -- o的命令输出
您可能需要IO.select 或其他IO 调度程序,或其他一些多任务调度程序,例如Thread。
这是Thread 方法的演示:
require 'open3'
Open3.popen3('ruby -e "while line = gets; print line; end"') do |i, o, t|
tin = Thread.new do
# here you can manipulate the standard input of the child process
i.puts "Hello"
i.puts "World"
i.close
end
tout = Thread.new do
# here you can fetch and process the standard output of the child process
while line = o.gets
print "COMMAND => #{line}"
end
end
tin.join # wait for the input thread
tout.join # wait for the output thread
end
【讨论】:
tee 的输出副本重定向到我的程序会怎样?它能解决我的问题吗?
STDIN.each_char {|c| i.puts c}那样把读取STDIN放在锡块里面?
tin块之前关闭子进程的标准输入(i.close)。
failed tty get。