Question

I am trying to run a bash script (@command) that requires user input and I'm trying to feed that script input using the following code:

Open3.popen3(@command) do |stdin, stdout, stderr|
  stdin.write("y")
  stdout.gets
end

Here is an idea of the script:

exec sudo su -c "some command" $username

If anyone could tell me what I am doing wrong or has any suggestions on how to implement this a different way, that would be much appreciated.


Also, I can run the script like this:

@output = `#{@command}`

In this case, I can see the contents of the script output in the console I am running my app from. If there is anyway to feed input to that location that would work too.

Was it helpful?

Solution

Got my solution here: How to fix hanging popen3 in Ruby?

Open3.popen3(@command) do |stdin, stdout, stderr|
  stdin.puts "y\r\n"

  stdout.each_line { |line| puts line }
  stdin.close
end

OTHER TIPS

out_err, status = Open3.capture2e(@command, :stdin_data => "y\r\n")
print out_err
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top