尝试放置错误变量时如何修复 Tcl 'Command not found' 错误?

How to fix Tcl 'Command not found' error while trying to puts error variable?

我正在尝试从另一个 expect 脚本 [=58= 执行 'expect script' abc ]xyz 并将其输出捕获到 $result 变量中。 abc 依次执行一个 AppleScript,其输出是回显的内容。在 abc 中,我正在尝试处理可以 expect[ 的场景=81=]-ed 来自 AppleScript。当 AppleScript returns 出现正结果时,我使用 exit 0 in abc,否则我使用 exit 1(希望 $result 变量 xyz 稍后可以检查退出状态和基于该状态执行的操作)。

我尝试使用

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 退出时脚本突然退出执行出口 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 中回显的内容,但实际输出要么以 'invalid command name' 为前缀(当使用 exit 0 in abc) 或附加 'child process exited abnormally'(当使用 exit 1 in abc).

编辑:当我尝试 puts 一些字符串而不是 $result[= 时,"invalid command name" 没有显示80=] 变量。

puts "How are you able to print this string without problem?"

显示时没有预先设置的错误消息。

当你这样做时:

catch {[exec bash -c "expect ~/Documents/bash/abc.sh $node"]} result

Tcl 执行 bash 命令,然后,因为 exec 在括号 ,所以 输出 bash 将作为 Tcl 命令执行.

另外,您根本不需要涉及 bash:

if {[catch {exec expect $env(HOME)/.../abc.exp $node} result] != 0} { ... }