user2883071
user2883071

Reputation: 980

TCL calling another proc after a proc

I have 2 procs which are called one after the other. The first proc uses the diff functionality and creates file.txt. The proc right after compresses the file. The issue is that when I run the script, the file.txt is empty. When I comment out the compressing proc, the file has the differences printed in it. I believe this is caused because the second proc does not wait for the first proc to finish.

create_difference "file1" "file2" 
create_compress "path"

The order of procedure calls above produce an empty file.txt.gz

create_difference "file1" "file2" 
#create_compress "path"

The order of procedure calls above create an expected difference file. Within the procs, I tried adding a return statement (to return 1), this did not make a difference.

I tried using the wait command as in Waiting for background processes to finish before exiting script:

create_difference "file1" "file2" 
wait
create_compress "path"

However the script hangs at that point.

Proc for creating the differences: tcl: capture output from "exec diff" which returned non-zero

set dso1 [open file.txt w]
set status [catch {exec diff $file1 $file2} result]
if {$status == 0} {
   puts $dso1 "$file1 and $file2 are identical"
} elseif {$status == 1} {
   puts $dso1 "** $file1 and $file2 are different **"
   puts $dso1 "***************************************************************************"
   puts $dso1 ""
   puts $dso1 $result
   puts $dso1 ""
   puts $dso1 "***************************************************************************"
} else {
   puts stderr "** diff exited with status $status **"
   puts stderr "***********************************************************************"
   puts stderr $result
   puts stderr "***********************************************************************"
}

The proc for compressing the files:

proc create_compress  {thepath} {
        catch {exec find ${thepath}/. -type f -exec gzip "{}" \; } result
        #return 1
}

There are some other files in there which need compressing, which is why I compress every file within the folder, given the path of the folder. The other files do compress as intended.

I ran some more testing on it. It seems that even after the diff proc has been called and finished, the differences are written to file.txt ONLY after the script has ended. Right up to the end of the script, the Differences file is created, but it's size is 0.

Upvotes: 0

Views: 1225

Answers (2)

user2883071
user2883071

Reputation: 980

The reason why nothing was being written to the Difference file is because it was not closed. In my create_diff proc, I forgot to include the command: close $dso1 Therefore once the script finished running, only then it would write to the file. However, right after the diff proc, I compress the files, and since it cannot write to a compressed file, the file would be empty.

Tldr; I did not close the file I was writing the differences to.

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247220

set status [catch {exec diff $file1 $file2} result]

"status" is not diff's exit status, it is catch's exit status. Not the same. To get diff's exit status:

if {$status == 1} {
    set err_type [lindex $::errorCode 0]
    if {$err_type eq "NONE"} {
        # diff exited with exit status 0 but printed something
        # to stderr
    } elseif {$err_type eq "CHILDSTATUS"} {
        # diff returned non-zero status
        set diff_exit_status [lindex $::errorCode end]
        if {$diff_exit_status == 1} {
            # diff results in $result
        } else {
            # diff had some kind of trouble
        }
    }
}

Upvotes: 0

Related Questions