How to Return Spawned Process Exit Code in Expect Script

How to get the exit code of spawned process in expect shell script?

With the help of glenn, I got solution.. and my final script is::

expect script is

 [Linux Dev:anr ]$ cat testexit.sh
#!/bin/bash
export tmp_script_file="/home/anr/tmp_script_temp.sh"
cp /home/anr/tmp_script $tmp_script_file
chmod a+x $tmp_script_file
cat $tmp_script_file
expect << 'EOF'
set timeout -1
spawn $env(tmp_script_file)
expect {
"INVALID " { exit 4 }
timeout { exit 4 }
eof
}

foreach {pid spawnid os_error_flag value} [wait] break

if {$os_error_flag == 0} {
puts "exit status: $value"
exit $value
} else {
puts "errno: $value"
exit $value
}
EOF
echo "spawned process status" $?
rm -f $tmp_script_file
echo "done"

Spawned script:

 [Linux Dev:anr ]$ cat tmp_script
exit 3

Execution of Expect script:

 [Linux Dev:anr ]$ ./testexit.sh
exit 3
spawn /home/anr/tmp_script_temp.sh
exit status: 3
spawned process status 3
done

Thanks Glenn once again..

How to return spawned process exit code in Expect script?

You're already waiting for the eof at the end of your loop, you just need to use wait and catch the result:

spawn true
expect eof
catch wait result
exit [lindex $result 3]

Exits with 0.

spawn false
expect eof
catch wait result
exit [lindex $result 3]

Exits with 1.

Expect: How to get the exit code from spawned process

You should write like this:

expect {
-re "Password: " {
send "$pass\r"
}
}
expect eof

catch wait result
exit [lindex $result 3]

or this:

expect {
-re "Password: " {
send "$pass\r"
exp_continue
}

eof {
catch wait result
}
}
exit [lindex $result 3]

According to expect's man page:

  • expect [[-opts] pat1 body1] ... [-opts] patn [bodyn]

    waits until one of the patterns matches the output of a spawned
    process, a specified time period has passed, or an end-of-file
    is seen. If the final body is empty, it may be omitted.
    Patterns from the most recent expect_before command are implicitly used before any other patterns. Patterns from the most
    recent expect_after command are implicitly used after any other
    patterns.

    If the arguments to the entire expect statement require more
    than one line, all the arguments may be braced into one so as
    to avoid terminating each line with a backslash. In this one
    case, the usual Tcl substitutions will occur despite the braces.

Capture exit code of Expect script

You have to handle the error by yourself. Try like this:

set err 0
send -- "put ${srcDir}/${XFILE} ${trgDir}/test_file.txt\r"
expect {
-re "File .* not found" {
set err 1
exp_continue
}
"sftp> "
}
send -- "bye\r"
expect eof

exit $err

Monitor spawned process for shell errors in Expect script

The inherent redirection in my scripts that is mentioned in the comments required more work that is irrelevant to this question, so I will simply post the answer to expecting KSH errors.

Prior to the ### Source the configuration file ### part, I added the following:

### Expect any KSH or function errors ###
expect_before -re "-ksh(.*): | (.*):(.*): " {set status 12; exit $status}

This essentially adds an expect -re block to all expect blocks following this code snippet, including those in procedures. It will expect the syntax that is typically seen for KSH or shell function errors when they arise.

Expect script failed with spawn id not open when calling expect eof


  1. Remove exp_continue after send "exit\r", otherwise EOF will be consumed by the expect { ... } block and another expect eof would fail.
  2. Change lindex \$result 3 to lindex $result 3 as in the comments.


Related Topics



Leave a reply



Submit