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.
Is there any way to control the input to a program invoked with system()
in ruby?
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.Use
Open3.open3
orOpen3.open2
to open up some pipes to your external process then write to thestdin
pipe just like writing to any other IO channel; if there is any output to deal with, then you can read it straight from thestdout
pipe just like reading from any other input IO channel.This can also be accomplished with IO.expect
This waits for the spawned process to display "0> " (the end of an irb prompt) and when it sees that prints a defined string. It then looks for the irb to return by waiting for it to display "=> " and grabs the data returned.
Something like this perhaps (using open as mu suggested)?