handling errors from spawn'ed commands in Expect 
Author Message
 handling errors from spawn'ed commands in Expect

I'm a casual expect user, and not very tclish at this point (the tcl I know
I've learned because of expect).  I have the Exploring Expect book and have
always been able to piece together what I need to do for any particular project
by index-surfing that.  That's failing me this time, though, so I'm hoping
for a bit of wisdom on what I'm doing wrong.  I'll happily RTFM, if someone
will give me a section of the FM to concentrate on!

I'm using expect 5.31 with tcl 8.3.0 on Solaris 2.6.  I need to spawn an
interactive command, passing it some arguments that were passed into the
script via the command line, catch any immediate errors that might happen
(no perms, no such file or directory, etc), then if there were no immediate
errors, go through a round of expect/send with the command.

Assuming the expect/send round goes ok, the command should do something.
If that something succeeds, the command sends something on stdout (which
I want my script to expect, and handle) and exits with a return value of 0.
If the "something" fails, the command sends something on stderr and exits
non-zero.  It's this second scenario that's causing me grief.

How do I monitor both stdout and stderr of a command I've spawn'ed?  How
do I get the return value from a command that I've spawn'ed?

Here's the relevant bits of the script (many of the variables are set higher
up):

if [catch "spawn $cmd $argv" reason] {
    puts stderr "$myname: failed to start $cmd:\n"
    puts stderr "\t$reason\n"
    exit 4

Quote:
}

match_max 100000
expect {
    "Enter SysOp password:"     {
        sleep .1
        send -s -- "$adminpw\r"
    }
    eof {
        puts stderr "$myname: $cmd exited immediately.\n"
        exit 5
    }

Quote:
}

expect -re {
        # The FAILED message would be on stderr, how do I catch it?
    "uniuser: *FAILED"      {
        puts stderr "$myname: $cmd failed.\n"
        exit 6
    }
    "uniuser: *added" {
        exit 0
    }

Quote:
}

Any and all suggestions appreciated.

Thanks,

Tim
--

Information Technology Services         (701) 231-1076 (Voice)
Room 242-J1, IACC Building              (701) 231-8541 (Fax)
North Dakota State University, Fargo, ND 58105-5164



Fri, 08 Nov 2002 03:00:00 GMT  
 handling errors from spawn'ed commands in Expect

Quote:

> How do I monitor both stdout and stderr of a command I've spawn'ed?  How
> do I get the return value from a command that I've spawn'ed?

check p. 174-5 and 292-3 of Don's book on how to make use of a shell's
redirection of stdout and stderr. by extension, you could make use
of the shell's return value mechanism also.

something like (not tested):

spawn /bin/sh -c "foo 2> error.out && echo success || echo fail"

Quote:

> Here's the relevant bits of the script (many of the variables are set higher
> up):

> expect -re {
>         # The FAILED message would be on stderr, how do I catch it?
>     "uniuser: *FAILED"      {
>         puts stderr "$myname: $cmd failed.\n"
>         exit 6
>     }
>     "uniuser: *added" {
>         exit 0
>     }
> }

> Any and all suggestions appreciated.

> Thanks,

> Tim
> --

> Information Technology Services         (701) 231-1076 (Voice)
> Room 242-J1, IACC Building              (701) 231-8541 (Fax)
> North Dakota State University, Fargo, ND 58105-5164

--
Aryeh Koenigsberg


Telrad Networks, Inc.
Lod, Israel


Sun, 10 Nov 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Expect spawn error handling

2. un'expect'ed eof

3. how to handle expect: spawn ssh two different things

4. question about compiling Expect script with freewrap: invalid command spawn

5. EXPECT, limit on length og command sent to a spawned ksh

6. Using Expect Spawn command inside a PROC

7. Getting a exit status from spawned command in expect

8. Expect: Will no Longer Spawn commands.

9. spawn command in expect

10. Using spawn command in Expect?

11. Strangeness from the arg'ed command line

12. expect_after executes for spawn id 1 even if the corresponding expect executes for spawn id 2

 

 
Powered by phpBB® Forum Software