2009-10-25 3 views
3

나는 tcl 스크립트를 가지고있다.tk, tcl exec stderr, stdout separate

문제는 내가 뭔가를 stderr (중요한 실패는 아닙니다)에 쓸 수있는 스크립트를 호출해야한다는 것입니다.

tk/tcl에서 stderr와 stdout을 별도로 캡처하고 싶습니다.

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

이 코드는 내 결과를 반환하지만 stderr도 포함합니다.

또한 변수 결과를 얻고 싶습니다. 사전에

감사합니다 ...

+0

사소한 문제 : 코드 예제에 닫는 대괄호를 포함하십시오. +1 큰 질문! –

답변

3

열려 경우

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

그런 다음 error.txt의 내용을 읽을 수 있습니다 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}" 
+0

감사합니다 ... 그 방법으로 일하고 있어요 ... – Egon

1

를 사용하여 2> 리디렉션 표준 오류 :

package require Tclx; # Needed for the read_file command 
set err [read_file error.txt] 
puts "s1: err = $err" 
+0

임시 파일없이 변수에 직접 가져 오기를 바랬습니다. 이 방법이 가능하다는 것을 알았지 만 임시 파일이 마음에 들지 않습니다. – Egon

관련 문제