質問

tclスクリプトがあります。

問題は、stderrに何かを書き込むことができるスクリプトを呼び出さなければならないことです(重大な障害ではありません)。

tk / tclでstderrとstdoutを別々にキャプチャしたい。

if { [catch {exec "./script.sh" << $data } result] } {
   puts "$::errorInfo"
}

このコードは結果を返しますが、stderrも含まれています。

また、結果を変数に取得したいです。

事前に感謝します...

役に立ちましたか?

解決

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}"

他のヒント

2を使用&gt; stderrをリダイレクトするには:

if { [catch {exec "./script.sh" << $data 2> error.txt} result } {
   puts "$::errorInfo"
}

その後、error.txtの内容を読むことができます:

package require Tclx; # Needed for the read_file command
set err [read_file error.txt]
puts "s1: err = $err"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top