-1

I want to make a ruby shell script that executes command from shell. What happens when the shell command needs the answer from a user?

For example, creating a new play framework application:

play new calimero

~        _            _ 
~  _ __ | | __ _ _  _| |
~ | '_ \| |/ _' | || |_|
~ |  __/|_|\____|\__ (_)
~ |_|            |__/   
~
~ play! 1.2.7.2, http://www.playframework.org
~

~ The new application will be created in /home/anquegi/src/11paths/buildsdeb/toni_build/devops-tools/packaging/packwithruby/calimero
~ What is the application name? [calimero] ~   <=== here asks to the user

With ruby shell script, I want to prompt this to the user and get it, the ruby script waits for that, I press enter, and it works.

value = %x(play new calimero)

waits until the user clicks intro. How I should manage this? Should I print the question before it is made?

sawa
  • 165,429
  • 45
  • 277
  • 381
anquegi
  • 11,125
  • 4
  • 51
  • 67

1 Answers1

2

Original: https://stackoverflow.com/a/6488335/2724079

This can also be accomplished with IO.expect

require 'pty'
require 'expect'

PTY.spawn("play new calimero") do |reader, writer|
  reader.expect(/What is the application name/)
  writer.puts("\n")
end

This waits for the spawned process to display "What is the application name" and when it sees that prints a defined string (new line).

Community
  • 1
  • 1
orzFly
  • 166
  • 1
  • 7
  • +1 for understanding a very confusing question, and for using two standard libraries I'd never seen before! – Max Aug 06 '15 at 12:42