Apologies for lack of sample code, I'm on mobile at the moment.
I've gotten ruby+open3 to run commands and save stdout and stderr to a variable.
My question is, if the command line interface prompts the user is it possible to input text into the prompt and press enter? If so how would I go about doing this.
Example explanation Runs program, program in terminal then asks "what is your name?" and waits for input.
I want to input a name, press enter.
Then it asks next question, I want to put to stdin and answer that as well
This is for an automation test. If anyone has a better idea than open3 I'm all ears but I'm restricted to ruby
Thanks
Consider this:
Create an input file using:
Then press CTRL+D to terminate the input, which will cause the file
test.input
to be created.In the same directory save this code as
test.rb
:Run the code using:
and you should see:
The reason this works is because
gets
reads the STDIN (by default) looking for a line-end, which in this case is the character trailingbar
andbaz
. If I load the input file in IRB it's easy to see the content of the file:2.times
says to read a line twice, so it reads both lines from the file and continues, falling out of thetimes
loop.This means you can create a file, pipe it into your script, and Ruby will do the right thing. If Ruby has called a sub-shell, the sub-shell will inherit Ruby's STDIN, STDOUT and STDERR streams. I can rewrite the test.rb code to:
and create
test.sh
:then call it using:
and get: