문제

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>를 사용하여 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