Reputation: 3
I am trying to execute an 'expect script' abc from another expect script xyz and capture its output in a $result variable. abc in turn executes an AppleScript whose output is something that is echo'd. In abc, I am trying to handle the scenarios that can be expect-ed from the AppleScript. When the AppleScript returns a positive outcome, I use exit 0 in abc, otherwise I use an exit 1 (with the hope that the $result variable of xyz can later be checked for the exit status and actions performed based on that status).
I tried using something like
set [exec bash -c "expect ~/Documents/bash/abc.sh $node"]
puts $result
instead of
{ [catch {[exec bash -c "expect ~/Documents/bash/abc.sh $node"]} result] } {
puts $result
}
which seems to work to my need, except that the script exits execution abruptly when abc exits with an exit 1.
This is abc, the script that calls the AppleScript.
#!/usr/bin/expect
# 'my_script' is an Applescript to execute. The output of my_script is going to be 'Already connected : XYZ' OR 'Unexpected error. Try again.' OR 'Connected : XYZ'
# When I say output, I mean it is going to echo something. Precisely, the Applescript has the following commands, onlh one of which is executed at a time, when a certain set of rules are satsfied.
# do shell script "echo Already connected : XYZ"
# do shell script "echo Unexpected error. Try again."
# do shell script "echo Connected : XYZ"
set my_script "~/Documents/bash/my.scpt"
spawn osascript $my_script $prodEnv
expect {
"Already connected : XYZ" { exit 0 } # If the ouput from the script is 'Already Connected : XYZ', then do nothing but just display it on the Terminal.
# This is where my problem begins. When I execute the 'exit 0' command, the intention is that the output from the script i.e., 'Already Connected : XYZ' must be displayed on the Terminal.
# However, that does happen, just not gracefully. It displays 'inavalid command name "Already Connected : XYZ"'
"Unexpected error. Try again." { exit 1 }
# The interesting part is, when the command executed is 'exit 1' like above, it is displayed alright on the Terminal, without the preceding 'invalid command name' that is.
# An additional encumbrance is that it also displays 'child process exited abnormally' in the following line.
"Connected : XYZ" { exit 0 }
# Again, the output here is 'invalid command name : "Connected : XYZ"'
}
This is xyz which calls abc.
#!/usr/bin/expect
set node [lindex $argv 0];
switch $node {
"node1" {
if { [catch {[exec bash -c "expect ~/Documents/bash/abc.sh $node"]} result] } {
# This puts is the source of the problem. When abc.sh exits with an 'exit 0' command, puts displays 'invalid command name'.
# However, when abc.sh exits with an 'exit 1' command, the shell can suddenly recognise the command.
puts "$result"
# The whole point of using catch is to allow abc.sh to use exit 1
# Here, I would like to do a check to see if abc.sh exited with a 0 or 1 status using $result. If exit status is 1, exit this script, otherwise continue execution.
}
}
# I have additional cases here.
}
# I have code here that must be executed only when _**abc**_ was not exited with an **exit 1**
I call xyz with
expect Documents/bash/xyz.sh mynode
I expect
puts $result
to display exactly what is echo'd from the AppleScript but the actual output is either prepended with an 'invalid command name' (when using exit 0 in abc) or appended with a 'child process exited abnormally' (when using exit 1 in abc).
EDIT: The "invalid command name" is not displayed when I try to puts some string instead of the $result variable.
puts "How are you able to print this string without problem?"
is displayed without a prepended error message.
Upvotes: 0
Views: 1988
Reputation: 246807
When you do this:
catch {[exec bash -c "expect ~/Documents/bash/abc.sh $node"]} result
Tcl with execute the bash
command, and then, because the exec
is inside brackets, the output of bash will be executed as a Tcl command.
Parenthetically, you don't need to involve bash at all:
if {[catch {exec expect $env(HOME)/.../abc.exp $node} result] != 0} { ... }
Upvotes: 2