On Apr 8, 9:32=A0am, Brian Candler <b.cand... / pobox.com> wrote:
> mpurdy wrote in post #991644:
>
> > echo -n "username: "
>
> You are sending just "username: " without a trailing newline.
>
> > =A0 =A0 =A0 =A0 =A0line =3D stdout.read
>
> Here you are reading from stdout until the end-of-file (i.e. until the
> other side terminates or closes the file). This will wait forever.
>
> Two possible solutions:
>
> 1. Change your shell script to send data ending with a newline, then use
> 'gets' in the ruby code.
>
> 2. More generally, use expect.rb in the standard library. Then you can
> wait for a particular sequence of characters, e.g. /name: /, before
> continuing.
>
> HTH,
>
> Brian.
>
> --
> Posted viahttp://www.ruby-forum.com/.

thanx, i looked into it and rewrote the script using the IO.expect and
it worked good. here is my scripts now:

(note: this is just to have an example for myself and team; we will
using ssh so all the passwords will be protected :-)

----------------------------------------------------------------------
bash script
----------------------------------------------------------------------
#! /bin/bash
echo -n "username: "
read -e USERNAME
echo -n "password: "
read -e PASSWORD
echo "running your script now for $USERNAME with password $PASSWORD"
echo 'doing something...'
sleep 1
echo 'end doing something...quiting'
exit 0


----------------------------------------------------------------------
ruby script
----------------------------------------------------------------------
require 'pty'    # found in ruby source @ /ext/pty
require 'expect' # found in ruby source @ /ext/pty/lib/expect.rb

#buffer stores the output from the child process
buffer =3D ""

# spawn a child process (fork)
PTY.spawn("./myScript.bash") do |output, input, pid|
   input.sync =3D true

   #set the expect verbosity flag to false or you will get output from
expect
   $expect_verbose =3D false

   #get user from environment if posible
   if !ENV['USER'].nil?
      username =3D ENV['USER']
   else
      username =3D 'guest'
   end

   #expect the username prompt and return the username
   output.expect('username: ') do
      input.puts(username)
   end

   #expect the password prompt and return the password
   output.expect('password: ') do
     input.puts 'thePassword'
   end

   #throw away the password comming back to thru the output (note: you
still get the '\n')
   output.expect('thePassword') do
   end

   #read all the output from the process and put it in a buffer for
later
   #keep reading until the EOFError exception is thrown
   done =3D 0
   while done =3D=3D 0
      begin
         buffer +=3D output.readpartial(1024)
      rescue EOFError
         done =3D 1
      end

   end

end

puts "myExcept.rb script ran myScript.bash the results are: \n
#{buffer}"