Expect: catch exit status

From FVue
Jump to: navigation, search

Problem

How can I – within expect – catch the exit status of an executed process?

Solution 1: spawn, close, wait

If the process was executed using spawn, call exp_wait after exp_close:

exp_close
exp_wait

    # Or example use in DejaGnu:
eval exp_spawn $cmd
set ret [exp_wait]
set pid         [lindex $ret 0]
set spawn_id    [lindex $ret 1]
set os_error    [lindex $ret 2]
set exit_status [lindex $ret 3]

Solution 2: exec, catch

If the process was executed using exec, use catch to investigate any error code.

expect1.1> catch [exec false] results
child process exited abnormally
    while executing
"exec false"
expect1.2> echo $errorCode
CHILDSTATUS 9551 1
expect1.3>

Or as tcl:

set status 0 
   # $results contains error messages
if {[catch [exec false] results]} { 
    if {[lindex $::errorCode 0] eq "CHILDSTATUS"} { 
        set status [lindex $::errorCode 2] 
    }   
}

Journal

20060213

Visited ActiveTcl - Online Docs : Tutorial

20071019

Re: spawn and redirect stderr
Forum question with 3-solution answer

Comments

blog comments powered by Disqus