# Expect Module and Bash script

**URL:** <https://rubytalk.org/t/expect-module-and-bash-script/35050>\
**Category:** ruby-talk\
**Created:** [3 February 2007 16:40 UTC](https://rubytalk.org/t/expect-module-and-bash-script/35050 "2007-02-03T16:40:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![R.B.Love](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@R.B.Love](https://rubytalk.org/u/R.B.Love)\
**Post date:** [3 February 2007 16:40 UTC](https://rubytalk.org/t/expect-module-and-bash-script/35050/1 "2007-02-03T16:40:05Z")

</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|  
&nbsp;&nbsp;write.sync = true  
&nbsp;&nbsp;read.sync = true  
&nbsp;&nbsp;$expect\_verbose = true # shows actions on screen  
&nbsp;&nbsp;  
&nbsp;&nbsp;read.expect("#? ") {write.puts "3"}  
&nbsp;&nbsp;&nbsp;&nbsp;  
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:** ![Bermejo\_Rodrigo](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@Bermejo\_Rodrigo](https://rubytalk.org/u/Bermejo_Rodrigo)\
**Post date:** [3 February 2007 19:27 UTC](https://rubytalk.org/t/expect-module-and-bash-script/35050/2 "2007-02-03T19:27:35Z")

</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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![R.B.Love](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@R.B.Love](https://rubytalk.org/u/R.B.Love)\
**Post date:** [4 February 2007 00:30 UTC](https://rubytalk.org/t/expect-module-and-bash-script/35050/3 "2007-02-04T00:30:09Z")

</div>

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.

> **···**
>
> On 2007-02-03 13:27:35 -0600, Rodrigo Bermejo \<rodrigo.bermejo@ps.ge.com\> said:
> 
> > 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`
