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..
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 recentexpect_before
command are implicitly used before any other patterns. Patterns from the most
recentexpect_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.
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.
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.
View exit code of a program (After the program exited)
There's a difference between an output
of a command, and the exit code of a command.
What you ran ./myprog 2> out
captures the stderr of the command and not the exit code as you showed above.
If you want to check the exit code of the a program in bash/shell you need to use the $?
operator which captures the last command exit code.
For example:
./myprog 2> out
echo $?
Will give you the exit code of the command.
BTW,
For capturing the output of a command, you may need to use 1
as your redirect where 1
captures stdout and 2
captures stderr.
Related Topics
Getting Message "Sudo: Must Be Setuid Root", But Sudo Is Already Owned by Root
Bash: Let Statement VS Assignment
What's the Difference Between Event-Driven and Asynchronous? Between Epoll and Aio
How to Display Modified Date Time with 'Find' Command
How to Delete First Two Lines and Last Four Lines from a Text File with Bash
How to Get Perf to Find Symbols in My Program
Recursively Find Files with a Specific Extension
Need Explanations for Linux Bash Builtin Exec Command Behavior
Linux Commands to Copy One File to Many Files
How Is It Possible That Kill -9 for a Process on Linux Has No Effect
Logo Programming Language Implementations
Split Output of Command by Columns Using Bash
Permission Denied When Trying to Append a File to a Root Owned File with Sudo
Shell Script Current Directory