Reputation: 6130
I'm building a small script where I'd like to launch the user's shell but redirect it's stdin and stdout so I can control them on the code. Would this be possible? I already tried with this code (which might be wrong though):
new_stdout, new_stdin = IO.pipe
pid = fork {
$stdout.reopen new_stdin
exec(ENV['SHELL'])
}
new_stdin.puts "Test"
Process.wait(pid)
This does nothing more than launch a new shell instance.
Thanks!
Upvotes: 2
Views: 2619
Reputation: 2764
While you may just use IO.pipe or Open3.popen3 method if simplicity is what your program requires. For advanced usage, I do it in cognizant project in the part, that you may refer to, where it demonizes a process and collects its standard input/output/error streams using Ruby 1.9's ::Process::spawn manually at https://github.com/Gurpartap/cognizant/blob/master/lib/cognizant/process/execution.rb
Upvotes: 0
Reputation: 146261
Something like this, perhaps?
IO.popen 'sh', 'r+' do |io|
io.puts 'echo how now brown cow | tr a-z A-Z'
result = io.gets
p [:result, result.size, result]
end
Upvotes: 3