Ego*_*gon 5 scripting tk-toolkit tcl
我有一个 tcl 脚本。
问题是我必须调用一个可以向 stderr 写入内容的脚本(这不是严重失败)。
我想在 tk/tcl 中分别捕获 stderr 和 stdout。
if { [catch {exec "./script.sh" << $data } result] } {
puts "$::errorInfo"
}
Run Code Online (Sandbox Code Playgroud)
此代码将返回我的结果,但它也包含 stderr。
我也想将结果传递给变量。
提前致谢...
如果将命令作为管道而不是 using 打开exec
,则可以将 stdout 和 stderr 分开。见http://wiki.tcl.tk/close
set data {here is some data}
set command {sh -c {
echo "to stdout"
read line
echo "$line"
echo >&2 "to stderr"
exit 42
}}
set pipe [open "| $command" w+]
puts $pipe $data
flush $pipe
set standard_output [read -nonewline $pipe]
set exit_status 0
if {[catch {close $pipe} standard_error] != 0} {
global errorCode
if {"CHILDSTATUS" == [lindex $errorCode 0]} {
set exit_status [lindex $errorCode 2]
}
}
puts "exit status is $exit_status"
puts "captured standard output: {$standard_output}"
puts "captured standard error: {$standard_error}"
Run Code Online (Sandbox Code Playgroud)