How Expect can capture the exit code from a remote command

LGT An example running on unix platform, we execute one command (for instance cd /tmp) then we send to the unix platform

 echo $? 

and we get the result on the next line as an integer

 package require Expect

 # some commands to connect to a unix platform should be inserted here before
 send "cd /tmp\r"
 # wait for the command to end : wait for the prompt $ followed by space
 expect "\\$ "
 send "echo \$?\r"
 expect -re "(\\d+)" {
     set result $expect_out(1,string)
 send_user "command exit with result code : $result"

Here is the output result

 $ cd /tmp
 $ echo $?
 $ command exit with result code : 0

echo $? is a shell command that will print the exit status of the previous shell command.

LV 2007 Nov 09 I suspect that the original writer had in mind that this type of thing is pretty commonly encountered by shell programmers - particularly if they have to use rsh or other remote execution commands.

The technique is that the writer of the code adds, to the shell commands being executed, commands to capture and then output the return code of the commands in question in a manner that could then be captured, parsed, and acted upon.

I would code some sort of example in shell, but that isn't necessarily relevant. Basically, I create the scripts in a file, then do the remote execution of those scripts, and make certain that appropriate return codes are displayed.