从 tcl 脚本调用 bash 脚本并返回和退出状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7177590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:39:36  来源:igfitidea点击:

Calling a bash script from a tcl script and returning and exit status

bashtcl

提问by tgai

I am attempting to call a bash script from a TCL script and need to get exit status from the bash script or at least pass something back into the TCL script so that I can tell if my script executed successfully. Any suggestions?

我正在尝试从 TCL 脚本调用 bash 脚本,并且需要从 bash 脚本中获取退出状态,或者至少将某些内容传递回 TCL 脚本,以便我可以判断我的脚本是否成功执行。有什么建议?

回答by glenn Hymanman

See http://wiki.tcl.tk/exec-- click the "Show discussion" button -- there's a very detailed example of how to do exactly what you're asking. What you need though is catch

请参阅http://wiki.tcl.tk/exec- 单击“显示讨论”按钮 - 有一个非常详细的示例,说明如何完全按照您的要求进行操作。你需要的是catch

set status [catch {exec script.bash} output]

if {$status == 0} {
    puts "script exited normally (exit status 0) and wrote nothing to stderr"
} elseif {$::errorCode eq "NONE"} {
    puts "script exited normally (exit status 0) but wrote something to stderr which is in $output"
} elseif {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
    puts "script exited with status [lindex $::errorCode end]."
} else ...

回答by jk.

What you want is execthe result of which will be in the return value, be warned however there are lots of gotchas using exec, particularly if you need to do any complex quoting

你想要的是exec ,其结果将在返回值中,但是有很多使用 exec 的问题,特别是如果你需要做任何复杂的引用,请注意

回答by GreenMatt

My experience in tcl is limited to occasional dabbling. However, following links starting with the one in @jk's answer led me to this pagewhich discusses the errorCode variable and related things that might be useful this circumstance. Here's a quick example demonstrating the use of errorCode:

我在 tcl 方面的经验仅限于偶尔涉足。但是,从@jk 回答中的链接开始,我进入了这个页面该页面讨论了 errorCode 变量和在这种情况下可能有用的相关内容。这是一个演示 errorCode 使用的快速示例:

tcl:

tcl:

set ret_val [catch { exec /bin/bash /path/to/bash_script }]
set errc $errorCode
set ret_val [lindex [split $errc " " ] 2]
puts $ret_val

bash_script, as referenced above:

bash_script,如上所述:

#!/bin/bash
exit 42

which led to output of:

这导致输出:

42

42