tcl:从“ exec diff”捕获输出。返回非零 [英] tcl: capture output from "exec diff" which returned non-zero

查看:219
本文介绍了tcl:从“ exec diff”捕获输出。返回非零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道执行可能返回非零的命令时通常使用catch ...,但是在这种情况下如何获取输出?

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具体来说,我希望执行捕获{exec diff fileA fileB} ret这样的操作。文件是不同的,ret的值为1。我真正需要的是diff的输出,详细的区别。但是我相信 catch {exec ...} err实践不能提供这种功能,对吧?

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?

有人可以建议您完成此任务吗?是否有tcl-builtin命令来执行文件差异? (我认为可以将输出重定向到文件,然后读取文件...还有其他选择吗?)

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?)

谢谢! XM

推荐答案

来自我最近的项目:

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

底线是,当文件不同时, status为1,$ result保存差异输出。在diff输出的末尾,我确实得到了子进程异常退出。就我而言,我还没有删除它,但是它应该很容易做到。

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.

这篇关于tcl:从“ exec diff”捕获输出。返回非零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆