我正在尝试从另一个期望脚本 xyz 中执行一个“期望脚本” abc 在 $ result 变量中输出。 abc 依次执行一个AppleScript,其输出是回显的内容。在 abc 中,我试图处理可以从AppleScript 期望 编辑的方案。当AppleScript返回肯定结果时,我在 abc 中使用 exit 0 ,否则,我使用 exit 1 (希望以后可以检查 xyz 的 $ result 变量的退出状态和执行的操作处于该状态)。
我尝试使用
之类的东西set [exec bash -c "expect ~/Documents/bash/abc.sh $node"]
puts $result
代替
{ [catch {[exec bash -c "expect ~/Documents/bash/abc.sh $node"]} result] } {
puts $result
}
这似乎很满足我的需要,只是当 abc 以 exit 1 退出时脚本突然退出执行。
这是 abc ,它是调用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"'
}
这是 xyz ,它调用 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**
我通过
呼叫 xyz
expect Documents/bash/xyz.sh mynode
我希望
puts $result
准确显示从AppleScript回显的内容,但实际输出之前带有“无效命令名称”(当在 abc 中使用 exit 0 时) strong> )或附加了“子进程异常退出”(在 abc 中使用退出1 时)。
编辑:当我尝试放入某些字符串而不是 $ result 变量时,未显示“无效的命令名称”。
puts "How are you able to print this string without problem?"
显示时未显示错误消息。
答案 0 :(得分:2)
执行此操作时:
catch {[exec bash -c "expect ~/Documents/bash/abc.sh $node"]} result
Tcl与执行bash
命令,然后,由于exec
在括号内 ,bash的输出将作为Tcl命令执行< / strong>。
从美学上讲,您根本不需要参与bash:
if {[catch {exec expect $env(HOME)/.../abc.exp $node} result] != 0} { ... }