【问题标题】:Use of diff command from tcl script captures error 'child process exited abnormally'使用 tcl 脚本中的 diff 命令捕获错误“子进程异常退出”
【发布时间】:2011-01-21 22:38:45
【问题描述】:

在 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 脚本中没有错误

【问题讨论】:

    标签: diff try-catch tcl


    【解决方案1】:

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

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

    但是你可以通过在你的命令中附加; true 来强制它工作。

    使其工作的更巧妙的方法是仅在退出代码 2: diff foo bar ; if [ $? -ne 2 ]; then true ; else false; fi; 时出错

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

    【讨论】:

    • 有人知道为什么我不能让if [ $? -neq 2 ] ; then true ; else false ; fi; 工作吗? Bash 返回:bash: [: -neq: binary operator expected
    • 不应该是-ne而不是-neq吗?
    • 谢谢@杰克逊!修复合并,因为这样更好。 :)
    • 所以,catch {exec sh -c {diff foo bar; [ $? -lt 2 ] &amp;&amp; true || false}} output
    • @vaichidrewar:非零退出代码被 Tcl 视为“异常”。这就是您收到“子进程异常退出”消息的原因。 Tcl 无法知道对于这个特定程序,退出状态 1 是“正常”,因此它报告异常终止。
    【解决方案2】:

    在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 page 上的讨论。

    【讨论】:

    • 执行上述代码时出现以下错误:表达式“[lindex $::errorCode 0] eq CHILDSTATUS”中的语法错误:编译时变量引用需要前面的$(“if”测试表达式) "如果 {[lindex $::errorCode 0] eq CHILDSTATUS} {
    • 啊,对,我应该在表达式中有一个“裸词”。我会解决的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    相关资源
    最近更新 更多