tcl: capture output from “exec diff” which returned non-zero

若如初见. 提交于 2020-01-02 10:02:56

问题


I know it is common to use catch when executing commands that may return non-zero... but how can I get the output in that case?

To be specific, I wish to do something like "catch {exec diff fileA fileB} ret". The files are different and ret value is 1. What I actaully need is the output of diff, the detailed differences. But I believe the "catch {exec ...} err" practice does not provide it, right?

Can someone please suggest on this task? Is there tcl-builtin commands to do file diff? (I think it is possible to redirect the output to a file and then read the file... are there any other alternatives?)

Thanks! XM


回答1:


From a recent project of mine:

set status [catch {exec diff $file1 $file2} result]
if {$status == 0} {
   puts "$file1 and $file2 are identical"
} elseif {$status == 1} {
   puts "** $file1 and $file2 are different **"
   puts "***************************************************************************"
   puts ""
   puts $result
   puts ""
   puts "***************************************************************************"
} else {
   puts stderr "** diff exited with status $status **"
   puts stderr "***********************************************************************"
   puts stderr $result
   puts stderr "***********************************************************************"
}

Bottom line, when the files are different, the status is 1 and $result holds the diff output. At the end of the diff output I do get the "child process exited abnormally". In my case I have not remove it, but it should be easy enough to do.



来源:https://stackoverflow.com/questions/3278197/tcl-capture-output-from-exec-diff-which-returned-non-zero

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!