# Needing to pipe commands to automate program

**URL:** <https://www.ruby-forum.com/t/needing-to-pipe-commands-to-automate-program/137961>\
**Category:** Ruby\
**Created:** [May 18, 2008, 11:51pm UTC](https://www.ruby-forum.com/t/needing-to-pipe-commands-to-automate-program/137961 "2008-05-18T23:51:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruggershawn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@Ruggershawn](https://www.ruby-forum.com/u/Ruggershawn)\
**Post date:** [May 18, 2008, 11:51pm UTC](https://www.ruby-forum.com/t/needing-to-pipe-commands-to-automate-program/137961/1 "2008-05-18T23:51:17Z")

</div>

Hello,

I am fairly new in the ruby language. I have a ruby program that  
needs to be automated. Every time I run this program you have to  
manually type in the questions.

Examples: Are you using 32/64 bit?  
Enter the path where you wish to install?

What I am trying to find a way where I can create a script to pipe the  
answers from a flat file so this can be done automatically.

Anyone have any ideas?

Thanks!

---

<div class="post-metadata">

**Author:** ![Ruggershawn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@Ruggershawn](https://www.ruby-forum.com/u/Ruggershawn)\
**Post date:** [May 19, 2008, 3:06am UTC](https://www.ruby-forum.com/t/needing-to-pipe-commands-to-automate-program/137961/2 "2008-05-19T03:06:51Z")

</div>

Ruggershawn wrote:

> Hello,
> 
> I am fairly new in the ruby language. I have a ruby program that  
> needs to be automated. Every time I run this program you have to  
> manually type in the questions.
> 
> Examples: Are you using 32/64 bit?  
> Enter the path where you wish to install?
> 
> What I am trying to find a way where I can create a script to pipe the  
> answers from a flat file so this can be done automatically.
> 
> Anyone have any ideas?
> 
> Thanks!

#original program: r1test.rb

puts “Enter 32 or 64 bit:”  
STDOUT.flush  
answer1 = gets.strip.chomp

puts “Enter the path where you wish to install:”  
STDOUT.flush  
answer2 = gets.strip.chomp

puts “Your answers were:”  
puts answer1  
puts answer2  
STDOUT.flush

* * *

#driver program: r2test.rb

pipe = IO.popen(“ruby r1test.rb”, “w+”)

f = File.open(“answers.txt”)

pipe.gets #Enter 32 or 64 bit:  
pipe.puts(f.gets.chomp)

pipe.gets #Enter the path:  
pipe.puts(f.gets.chomp)

puts pipe.gets, pipe.gets, pipe.gets

puts  
puts “Hit enter to end program”  
gets

* * *

$ ruby r2test.rb  
Your answers were:  
32  
./test\_programs

Hit enter to end program

---

<div class="post-metadata">

**Author:** ![Ruggershawn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@Ruggershawn](https://www.ruby-forum.com/u/Ruggershawn)\
**Post date:** [May 19, 2008, 9:25am UTC](https://www.ruby-forum.com/t/needing-to-pipe-commands-to-automate-program/137961/3 "2008-05-19T09:25:52Z")

</div>

Ruggershawn wrote:

> Hello,
> 
> I am fairly new in the ruby language. I have a ruby program that  
> needs to be automated. Every time I run this program you have to  
> manually type in the questions.
> 
> Examples: Are you using 32/64 bit?  
> Enter the path where you wish to install?
> 
> What I am trying to find a way where I can create a script to pipe the  
> answers from a flat file so this can be done automatically.
> 
> Anyone have any ideas?
> 
> Thanks!

This works too:

#original program: r1test.rb

puts “Enter 32 or 64 bit:”  
answer1 = gets.strip.chomp

puts “Enter the path where you wish to install:”  
answer2 = gets.strip.chomp

File.open(‘output.txt’, ‘w’) do |f|  
f.puts(“Your answers were:”)  
f.puts answer1  
f.puts answer2  
end

Just be aware that popen() hijacks stdin and stdout in the original  
program–directing the original program’s input and output to the IO  
object created in the driver program. If your original program writes  
some results to a file, then that won’t be an issue.

---

<div class="post-metadata">

**Author:** ![Ruggershawn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@Ruggershawn](https://www.ruby-forum.com/u/Ruggershawn)\
**Post date:** [May 19, 2008, 9:27am UTC](https://www.ruby-forum.com/t/needing-to-pipe-commands-to-automate-program/137961/4 "2008-05-19T09:27:12Z")

</div>

7stud – wrote:

> This works too:
> 
> #original program: r1test.rb
> 
> puts “Enter 32 or 64 bit:”  
> answer1 = gets.strip.chomp
> 
> puts “Enter the path where you wish to install:”  
> answer2 = gets.strip.chomp
> 
> File.open(‘output.txt’, ‘w’) do |f|  
> f.puts(“Your answers were:”)  
> f.puts answer1  
> f.puts answer2  
> end
> 
> Just be aware that popen() hijacks stdin and stdout in the original  
> program–directing the original program’s input and output to the IO  
> object created in the driver program. If your original program writes  
> some results to a file, then that won’t be an issue.

Whoops. Here’s the driver program:

#driver program:

pipe = IO.popen(“ruby r1test.rb”, “w+”)  
f = File.open(“data.txt”)

pipe.puts(f.gets.chomp)  
pipe.puts(f.gets.chomp)

puts  
puts “Hit enter to end program”  
gets

---

<div class="post-metadata">

**Author:** ![Ruggershawn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@Ruggershawn](https://www.ruby-forum.com/u/Ruggershawn)\
**Post date:** [May 22, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/needing-to-pipe-commands-to-automate-program/137961/5 "2008-05-22T23:27:25Z")

</div>

Hey Thanks for the reply! It worked great!

Now I am wondering how to output the standard out to the screen? Right  
now I run my ruby program and cant see anything being logged.
