问题
I want to use system("insert shell command here") and pass in a script. This script however, requires user input. I want to pass in the user input as well. How do I do this?
I tried:
system('./script')
system('input1')
system('input2')
However, Ruby waits till the first system call is done and then only proceeds to go to the next one.
Is it possible to do this? I've tried system('./script', 'input1') but that didn't work either.
No luck with exec either.
回答1:
You should look into the Open3 library from the Ruby stdlib. It gives you more control over "shelling out".
output, status = Open3.capture2("./script", :stdin_data => "I am STDIN")
回答2:
#!/usr/bin env ruby
input1 = ARGV[0]
input2 = ARGV[1]
system("./script #{input1} #{input2}")
来源:https://stackoverflow.com/questions/17956663/how-do-i-use-the-system-command-to-pass-in-user-input