使用 tcl 脚本中的 diff 命令捕获错误“子进程异常退出”

发布于 2024-10-13 04:13:57 字数 524 浏览 3 评论 0原文

在 tcl 脚本中,我使用 diff 命令逐行比较文件

if {[catch {eval exec "diff /tmp/tECSv2_P_HTTP_XHDR_URL_FUNC_12.itcl /tmp/tempReformat"} results]} {

    puts "$results"
}

diff 命令的输出已正确获得,但它捕获错误“子进程异常退出”

输出:

==>tclsh diffUsingScript

992c992
<             fail "Redirection is not reflected in css messages"
---
>         fail "Redirection is not reflected in css messages"
child process exited abnormally

那么,由于获得此错误,所以出了什么问题。我希望 diff 操作在我的 tcl 脚本中没有错误

In tcl script I am using diff command to compare the files line by line

if {[catch {eval exec "diff /tmp/tECSv2_P_HTTP_XHDR_URL_FUNC_12.itcl /tmp/tempReformat"} results]} {

    puts "$results"
}

Output of diff command is obtained properly but it catches error 'child process exited abnormally'

Output:

==>tclsh diffUsingScript

992c992
<             fail "Redirection is not reflected in css messages"
---
>         fail "Redirection is not reflected in css messages"
child process exited abnormally

So whats going wrong due to which this error is obtained. I want diff operation to be error free in my tcl script

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

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

发布评论

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

评论(2

一口甜 2024-10-20 04:13:57

来自我的 diff(1):“如果输入相同,则退出状态为 0,如果不同,则退出状态为 1,如果出现问题,则退出状态为 2。”

由于非零返回是 shell 脚本中报告错误的常用方式,因此 tcl 和 diff 在返回结果的含义上存在分歧。编写 shell 脚本来了解两个文件是否与返回值不同可能真的很方便,但我没有看到任何机制可以从联机帮助页中禁用它。 (我宁愿使用 cmp -q 来获取两个文件是否不同,不确定为什么 diff 人员做出了他们所做的决定。)

但是您可以通过附加 <代码>; true 符合您的命令。

让它工作的更巧妙的方法是仅在退出代码为 2 时出错: diff foo bar ;如果 [ $? -ne 2 ];那么正确;否则为假; fi;

在每次测试后检查不同文件名的结果并 echo $? 以查看哪些文件返回 0(来自 true )以及哪些返回 1 (来自 false)。

From my diff(1): "Exit status is 0 if inputs are the same, 1 if different, 2 if trouble."

Since non-zero returns are the usual way to report errors in shell scripts, tcl and diff disagree on the meaning of the return result. It's probably really convenient for writing shell scripts to know whether or not two files are different from the return value, but I don't see any mechanism to disable that from the manpage. (I'd rather use cmp -q for just getting whether or not two files are different, not sure why the diff people made the decision they did.)

But you can bludgeon this into working by appending ; true to your command.

A more artful way to make it work would be to error only on an exit code of 2: diff foo bar ; if [ $? -ne 2 ]; then true ; else false; fi;

Check the results with different filenames and echo $? after each test to see which ones are returning 0 (from true) and which ones are returning 1 (from false).

国粹 2024-10-20 04:13:57

在 Tcl 中处理这个问题的方法是:

set rc [catch {exec diff f1 f2} output]
if {$rc == 0} {
    puts "no difference"
} else {
    if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
        if {[lindex $::errorCode 2] == 1} {
            puts "difference"
            # show output without "child process exited abnormally" message
            puts [string replace $output end-31 end ""]
        } else {
            puts "diff error: $output"
        }
    } else {
        puts "error calling diff: $output"
    }
}

参见 Tcl Wiki exec 页面上的讨论。

The way to handle this in Tcl is:

set rc [catch {exec diff f1 f2} output]
if {$rc == 0} {
    puts "no difference"
} else {
    if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
        if {[lindex $::errorCode 2] == 1} {
            puts "difference"
            # show output without "child process exited abnormally" message
            puts [string replace $output end-31 end ""]
        } else {
            puts "diff error: $output"
        }
    } else {
        puts "error calling diff: $output"
    }
}

See the discussion on the Tcl Wiki exec page.

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