问题描述
我正在尝试使用 net/sftp 下载文件并将其内容作为命令行应用程序的标准输入传递.我可以先将文件写入磁盘,但我宁愿避免这一步.
I'm trying to download a file using net/sftp and pass its contents as the stdin for a command-line app. I can do it by first writing the file to disk but I'd rather avoid that step.
有没有办法控制在 ruby 中使用 system()
调用的程序的输入?
Is there any way to control the input to a program invoked with system()
in ruby?
推荐答案
不要使用 system
对于这类事情,system
最适合运行您不需要与之交谈的外部命令.
Don't use system
at all for this sort of thing, system
is best for running an external command that you don't need to talk to.
使用 Open3.open3
或 Open3.open2
向外部进程打开一些管道,然后像写入任何其他 IO 通道一样写入 stdin
管道;如果有任何输出需要处理,那么您可以直接从 stdout
管道中读取它,就像从任何其他输入 IO 通道读取一样.
Use Open3.open3
or Open3.open2
to open up some pipes to your external process then write to the stdin
pipe just like writing to any other IO channel; if there is any output to deal with, then you can read it straight from the stdout
pipe just like reading from any other input IO channel.
这篇关于在 ruby 中设置 system() 调用的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!