I am running a command in expect script and I want to exit the expect script if command return value is not 0. I set the variable ret to "$?" like this, which is not working:
send "./$SCRIPT_NAME >> out.txt &\r"
expect "~#"
set ret $?
if { $ret != 0} {
exit $ret
}
But I get this output:
expected integer but got "$?"
while executing
"exit $ret"
invoked from within
"if { $ret != 0} {
exit $ret
}"
What is the equivalent of $? in expect?
This is a bit of a pain in expect.
Firstly, since you need to get the exit status, you don't want to launch the script in the background. You'd have to use the shell's
waitcommand anyway to get the exit status.Next, you have to ask the shell to print out the exit status and capture that with the
expectcommand. Note that expect returns\r\nfor line endings.