Domanda

Ho uno script tcl.

Il problema è che devo chiamare uno script che può scrivere qualcosa su stderr (non è un errore critico).

Vorrei catturare stderr e stdout separatamente in tk / tcl.

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

Questo codice restituirà il mio risultato ma contiene anche stderr.

Inoltre vorrei ottenere il risultato in una variabile.

Grazie in anticipo ...

È stato utile?

Soluzione

Se apri il comando come pipe invece di usare exec , puoi separare stdout e stderr. Vedi 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}"

Altri suggerimenti

Utilizza il 2 > per reindirizzare stderr:

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

È quindi possibile leggere il contenuto di error.txt:

package require Tclx; # Needed for the read_file command
set err [read_file error.txt]
puts "s1: err = $err"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top