从 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
Calling a bash script from a tcl script and returning and exit status
提问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.
回答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

