0

I have a bash script which internally calls a Ruby script, based on the situation sometimes the Ruby script will prompt for a user selection; how can I make this possible?

Main Bash script:

#!/usr/bash  
for host_name in `./myruby.rb -a xxx -b xxx`    
do  
    echo -e "$host_name"
done

wrapper myruby.rb

#!/usr/bin/ruby  
.....  
......   
puts "enter your selection ?"  
user_selection = $stdin.gets  

If I run the Ruby directly, it's working fine, but when I run it from bash it's not prompting for user input; rather it's hanging.

2
  • 1
    Your script isn't hanging; it's just waiting for input. You don't see the prompt (I suspect) because it is printed to standard output, which is captured by the backticks. Commented Jan 30, 2013 at 13:00
  • Note that the first four host names will be 'enter', 'your', 'selection' and '?' because your Ruby script writes its prompt to standard output. Perhaps you should write the prompt to standard error instead; the user will see that and be able to respond, and the prompt won't be captured by the backticks either. Commented Jan 30, 2013 at 13:05

1 Answer 1

1

The ruby script reads from stdin, so you need to provide something on stdin even when called from bash. you could do it like this:

$(rubyscript <<< "selection") 

The $() construction is the same as backticks The <<< provides the string "selection" as input on stdin for the rubyscript.

Or you could just type the answer yourself when the bash script "hangs"

Sign up to request clarification or add additional context in comments.

2 Comments

The 'or you could just type the answer' part applies, I think. The shell is capturing the prompt as the first four the host names, and then waiting for the user to respond.
the ruby script will generate the host-names based on the user input, more over i can't pass arguments on bacticts, because based on situation ruby script will prompt for user input some time it won't prompt, my requirement is if a ruby script prompt for user input how can i capture it on bash script?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.