Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

expect and multiple processes

Status
Not open for further replies.

Mathias77

Programmer
Nov 18, 2003
1
SE
Hi,

I'm a rookie trying to learn expect
I have a question:
If I want my expect script to handle two processes at a time but in two different terminals how do I do that? I have tried by having the first process executing in the same terminal as I started the script and the second to work in a new terminal by spawning a new terminal and then saving the spawn_id. But when I want to send to this process I had no luck. This what was I was trying to test:

---------
spawn dtterm -geometry 80x40+63+13 -title test -fg green -bg black

send "ls\r"

---------

The 'ls' command was never executed in the new terminal...

Can anyone give a quick example that works involving a new terminal and sending messages to both processes?

Regards,
Mathias
 
You are talking about xterm like processes?
If so this is not really expects forte.
Yes, it can be done, but there is really
no reason for it to ever be done IMHO.

You should obtain a copy of 'exploring expect'
and look at the xkibitz example on pg361.
If you need a straightforward example of using
expect to control two spawned programs, try
something like this:

Code:
!/usr/bin/tclsh
package require Expect

#uncomment below for debugging
#exp_internal 1
proc dochange {} {
global shellid1 shellid2 spawn_id

   send_user "Switch to $shellid1 (?): "
   set ans [gets stdin]
  
    if {[string compare $ans "yes"] == 0} {
        set spawn_id $shellid1
        puts "\nIn spawn_id: $shellid1"
    } else {
        set spawn_id $shellid2
        puts "\nIn spawn_id: $shellid2"
    }
}



#exp_internal 1
set prompt ".*@.*"
spawn -noecho /bin/sh
set shellid1 $spawn_id
spawn -noecho /bin/sh
set shellid2 $spawn_id

      expect -re "$prompt" {
                  send_user "Attached to $spawn_id\n"
                  interact  {
                     "XX" {dochange}
                   }
       }


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top