vaichidrewar
vaichidrewar

Reputation: 9621

Use of diff command from tcl script captures error 'child process exited abnormally'

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

Upvotes: 1

Views: 4895

Answers (2)

glenn jackman
glenn jackman

Reputation: 247142

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.

Upvotes: 0

sarnold
sarnold

Reputation: 104090

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

Upvotes: 1

Related Questions