util::catch_exec (public)

 util::catch_exec command result_var

Defined in packages/acs-tcl/tcl/utilities-procs.tcl

Catch a call to Tcl exec. Handle shell return codes consistently. Works like catch. The result of the exec is put into the variable named in result_var. Inspired by https://wiki.tcl-lang.org/1039

Parameters:
command (required)
A list of arguments to pass to exec
result_var (required)
Variable name in caller's scope to set the result in
Returns:
0 or 1. 0 if no error, 1 if an error occurred. If an error occurred the error message will be put into result_var in the caller's scope.
Author:
Dave Bauer
Created:
2008-01-28

Partial Call Graph (max 5 caller/called nodes):
%3

Testcases:
No testcase defined.
Source code:

    upvar result_var result
    set status [catch [concat exec $command] result]
    if { $status == 0 } {

        # The command succeeded, and wrote nothing to stderr.
        # $result contains what it wrote to stdout, unless you
        # redirected it
        ns_log debug "util::catch_exec: Status == 0 $result"

    } elseif {$::errorCode eq "NONE"} {

        # The command exited with a normal status, but wrote something
        # to stderr, which is included in $result.
        ns_log debug "util::catch_exec: Normal Status $result"

    } else {

        switch -exact -- [lindex $::errorCode 0] {

            CHILDKILLED {
                lassign $::errorCode  - pid sigName msg

                # A child process, whose process ID was $pid,
                # died on a signal named $sigName.  A human-
                # readable message appears in $msg.
                ns_log notice "util::catch_exec: childkilled $pid $sigName $msg $result"
                set result "process $pid died with signal $sigName \"$msg\""
                return 1
            }

            CHILDSTATUS {

                lassign $::errorCode  - pid code

                # A child process, whose process ID was $pid,
                # exited with a nonzero exit status, $code.
                ns_log notice "util::catch_exec: Childstatus $pid $code $result"
            }

            CHILDSUSP {

                lassign $::errorCode  - pid sigName msg

                # A child process, whose process ID was $pid,
                # has been suspended because of a signal named
                # $sigName.  A human-readable description of the
                # signal appears in $msg.
                ns_log notice "util::catch_exec: Child susp $pid $sigName $msg $result"
                set result "process $pid was suspended with signal $sigName \"$msg\""
                return 1
            }

            POSIX {

                lassign $::errorCode  - errName msg

                # One of the kernel calls to launch the command
                # failed.  The error code is in $errName, and a
                # human-readable message is in $msg.
                ns_log notice "util::catch_exec: posix $errName $msg $result"
                set result "an error occurred $errName \"$msg\""
                return 1
            }

        }
    }
    return 0
XQL Not present:
PostgreSQL, Oracle
Generic XQL file:
packages/acs-tcl/tcl/utilities-procs.xql

[ hide source ] | [ make this the default ]
Show another procedure: