# Expect Module and Bash script

**URL:** <https://www.ruby-forum.com/t/expect-module-and-bash-script/84363>\
**Category:** Ruby\
**Created:** [February 3, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/expect-module-and-bash-script/84363 "2007-02-03T17:40:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![R.B.Love](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c68b51/32.png) [@R.B.Love](https://www.ruby-forum.com/u/R.B.Love)\
**Post date:** [February 3, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/expect-module-and-bash-script/84363/1 "2007-02-03T17:40:50Z")

</div>

How do I use ruby expect to feed input to a simple bash script? Here  
is a minimal example of what I’m trying to do. All help appreciated.

First, the simple bash script. When run from the command line it  
performs as I expect.

#!/bin/bash  
echo “1) red”  
echo “2) blue”  
echo “3) green”  
echo "#? "  
read CHOICE  
sleep 5  
echo “$CHOICE is done”  
exit 0

Now for the ruby script I want to work with the bash script

#!/usr/bin/env ruby

require ‘expect’  
require ‘pty’

p = PTY.spawn(’./simple’) do |read,write,pid|  
write.sync = true  
read.sync = true  
$expect\_verbose = true # shows actions on screen

read.expect("#? ") {write.puts “3”}

end

When ruby operates the bash script, I doesn’t seem like the input is  
given to bash.

I’ve tried ruby 1.8.4 on OSX and ruby 1.8.5 on Fedora Core 6.

---

<div class="post-metadata">

**Author:** ![R.B.Love](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c68b51/32.png) [@R.B.Love](https://www.ruby-forum.com/u/R.B.Love)\
**Post date:** [February 3, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/expect-module-and-bash-script/84363/2 "2007-02-03T20:27:33Z")

</div>

R.B.Love wrote:

> How do I use ruby expect to feed input to a simple bash script? Here  
> is a minimal example of what I’m trying to do. All help appreciated.
> 
> First, the simple bash script. When run from the command line it  
> performs as I expect.
> 
> #!/bin/bash  
> echo “1) red”  
> echo “2) blue”  
> echo “3) green”  
> echo "#? "  
> read CHOICE  
> sleep 5  
> echo “$CHOICE is done”  
> exit 0

tricky but should work

#!/usr/bin/ruby  
`script_to_feed<<END red END`

-rb.

---

<div class="post-metadata">

**Author:** ![R.B.Love](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c68b51/32.png) [@R.B.Love](https://www.ruby-forum.com/u/R.B.Love)\
**Post date:** [February 4, 2007, 1:30am UTC](https://www.ruby-forum.com/t/expect-module-and-bash-script/84363/3 "2007-02-04T01:30:30Z")

</div>

On 2007-02-03 13:27:35 -0600, Rodrigo B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > echo “3) green”  
> > #!/usr/bin/ruby  
> > `script_to_feed<<END red END`

Uh, thanks but it doesn’t use the expect module at all. Does it not  
work? I have a lot more I want to do with my script, this was just a  
minimal example.

I assume the failure is in my understanding, not the expect process.
