【问题标题】:How to fix Tcl 'Command not found' error while trying to puts error variable?如何在尝试放置错误变量时修复 Tcl 'Command not found' 错误?
【发布时间】:2019-08-17 13:57:01
【问题描述】:

我正在尝试从另一个期望脚本 xyz 执行“期望脚本”abc 并捕获其$result 变量中的输出。 abc 依次执行一个 AppleScript,其输出是经过回显的。在 abc 中,我正在尝试处理 AppleScript 中可能 expect 的场景。当 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
}

这似乎符合我的需要,除了当 abcexit 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 中使用 exit 1 时)。

编辑:当我尝试 puts 一些字符串而不是 $result 变量时,不显示“无效的命令名称”。

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

显示时没有附加错误消息。

【问题讨论】:

    标签: linux bash shell tcl expect


    【解决方案1】:

    当你这样做时:

    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} { ... }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-09
      • 2019-09-05
      • 2020-09-26
      • 2021-12-14
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多