tcl:捕获“exec diff”的输出返回非零

发布于 2024-09-10 13:32:19 字数 284 浏览 3 评论 0原文

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

具体来说,我想做一些类似“catch {exec diff fileA fileB} ret”的事情。文件不同,ret值为1。我实际上需要的是diff的输出,即详细的差异。但我相信“catch {exec ...} err”实践并没有提供它,对吧?

有人可以建议这项任务吗?是否有 tcl 内置命令可以进行文件比较? (我认为可以将输出重定向到文件,然后读取该文件......还有其他选择吗?)

谢谢! XM

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人九事 2024-09-17 13:32:19

来自我最近的一个项目:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文