I have a Bash script which includes an expect/TCL EOF script in a separate function. The expect script exits with 0-4 possible exit codes depending on what the expect/TCL script determines from the remote device, and within an if ... elif ... else statement I write a specific string to a variable depending on this exit code (within that expect/TCL function). Control is then passed back to the Bash script where a case block runs on the string contained within said variable contains.
The trouble I am experiencing is that my Bash function which contains the expect/TCL script does not catch exit code 3 (well actually it does as I can see the log file entry correctly writes to the log file, but if I echo the value of the exit code it actually catches a zero when the situation should be a 3) and thus my case statement is not switching accurately.
Can you find the bug?
(I've chopped the script right down to just these parts for purpose of keeping the post concise and specific so just assume that the surrounding code is running OK).
function myTclFunc()
{
/usr/bin/expect<<EOF
proc log_msg {msg {to_stdout no}} {
set log_line "[timestamp -format {[%d/%m/%Y @ %T]}] \$msg"
set fh [open ~/mylogfile.log a]
puts \$fh \$log_line
close \$fh
if {\$to_stdout} {puts \$log_line}
}
;#exp_internal 1
set timeout 5
set send_human {.1 .3 1 .05 2}
spawn ssh -o "StrictHostKeyChecking no" "[USER]@$1"
expect {
"password: " { send -h "[MY_PASSWD]\r" }
timeout { log_msg "A RELEVANT STRING TO LOG $1 / $2"; exit 1 }
}
set timeout 3
sleep 1 ;
send -h "[COMMAND A]\r" ;
expect {
timeout { exit 1 }
-re {\m\d{1,}(\.\d{1,}){3}\M}
}
if { ! [regexp {192\.[0-9]{1,3}\.{2}[0-9]{1,3}} $expect_out(0,string)]} {
send -h "[COMMAND B]\r" ;
}
expect {
"[STRING 1]" {
send -h "[COMMAND C]\r" ;
log_msg "Problem F on $1 / $2" ;
exit 3
}
"[STRING 2]" {
send -h "[COMMAND D]\r" ;
sleep 1 ;
send -h "[COMMAND E]\r" ;
send -h "\r" ;
puts "\r"
;#exit 0
}
}
expect eof
EOF
if [[ $? -eq 0 ]]; then
passBack="GOOD";
elif [[ $? -eq 3 ]]; then
passBack="BAD";
else
passBack="TIMEOUT";
fi;
}
[...snipped code...]
myTclFunc $myVar $1
case "$passBack" in
GOOD)
echo ""
exit 0
;;
BAD)
echo ""
exit 4
;;
CHECK)
echo ""
exit 3
;;
esac;
[...snipped code...]