gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:10:22 24 4
gpt4 key购买 nike

我正在尝试从另一个期望脚本 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
}

这似乎符合我的需要,除了当 abcexit 1 退出时脚本突然退出执行。

这是调用 AppleScript 的脚本 abc

#!/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"'
}

这是调用 abcxyz

#!/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 时)。

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

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

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

最佳答案

当你这样做时:

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

Tcl 执行 bash 命令,然后,因为 execinside brackets,bash 的输出将是作为 Tcl 命令执行

顺便说一句,您根本不需要涉及 bash:

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

关于linux - 尝试放置错误变量时如何修复 Tcl 'Command not found' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57536947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com