문제

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.

도움이 되었습니까?

해결책

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

다른 팁

out_err, status = Open3.capture2e(@command, :stdin_data => "y\r\n")
print out_err
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top