Hello,
I have a ruby script that executes a linux command however the command
returns a question:
This will update 1 entry, continue? [y/N]:
I can see this output in when in debug mode for the script. How can I
pass a y and return/enter automatically for each time this question
comes up? I tried to add system(echo y) and puts “\r” to send a y and do
a return but no success. Any pointers?
Here is an example of my small script:
system(“opsdb --dry-run --server nventory.local nv --exactget
name=dhcpserver.local --addtonodegroup=temp1,temp2”)
system(echo y)
puts “\r”
Richard S. wrote in post #971231:
Hello,
snip
system(“opsdb --dry-run --server nventory.local nv --exactget
name=dhcpserver.local --addtonodegroup=temp1,temp2”)
system(echo y)
puts “\r”
This might help:
http://tech.natemurray.com/2007/03/ruby-shell-commands.html
Also, something like this might work:
%Q{sh -c ‘echo “Y” | yourcomamnd’}
On 12/29/2010 12:36 AM, Richard S. wrote:
a return but no success. Any pointers?
mdb = YAML::load_file(‘mdb_dump.yml’)
system(echo y)
puts “\r”
The system method runs the child program non-interactively from the
perspective of your script. You’ll want to look at IO.popen. That will
run the child and return its stdin and stdout as a duplexed IO object
for you. You can then use something like the expect library (part of
the Ruby standard library) to watch the program’s output for the prompt
at which point you can send whatever you like using puts or print on the
IO object popen gave you.
require ‘expect’
child_io = IO.popen(<<-BASH, ‘r+’)
bash -c "
printf 'the prompt> ’
read
echo The user typed \\\"\"\$REPLY\"\\\"
"
BASH
child_io.expect('the prompt> ') { child_io.puts ‘this is my response’ }
puts child_io.read
#-> The user typed “this is my response”
-Jeremy
Adam Ms. wrote in post #971304:
Richard S. wrote in post #971231:
Hello,
snip
system(“opsdb --dry-run --server nventory.local nv --exactget
name=dhcpserver.local --addtonodegroup=temp1,temp2”)
system(echo y)
puts “\r”
This might help:
tech.natemurray.com: 6 Ways to Run Shell Commands in Ruby
Also, something like this might work:
%Q{sh -c ‘echo “Y” | yourcomamnd’}
Or perhaps:
system("yes | opsdb … ")
See “man yes” for info.