Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling C Shell scripts from TCL

Status
Not open for further replies.

mollyvg

Programmer
Jul 5, 2001
2
US
When the users clicks on a button, I want to call a c shell script which could have 1 to many globally stored arguments and then retrieve the error code returned by the script. The error code is then used later in the project.
I'm am using vtcl.
Does anyone know the quick way to do this?
Thanks,
Molly
 
I don't know about vtcl-I use the expect extensions to tcl I would write the return code to a buffer file in the script, wrap it like so, and then retrieve it.

proc get_ret_id {} {
global err
set timeout -1
set holdf [open "/tmp/buf" r]
spawn -noecho /bin/csh scriptname
interact {
eof {close}
}
if {[get $holdf line] != -1} {
set err [puts $line]
}
}
It seems to me that you will have to do something like this
to retrieve another processes exit code though I could certainly be very wrong, I'm not an expert by any means.
 
I'm not sure what you meant by "I want to call a c shell script which could have 1 to many globally stored arguments", so I can't help you with that. But as for retrieving the "error code", if you mean the exit status of the program executed, Tcl can get that information if you run the C shell scripts with Tcl's exec command.

First of all, from the exec manual page:

"If standard output has not been redirected then the exec command returns the standard output from the last command in the pipeline. If any of the commands in the pipeline exit abnormally or are killed or suspended, then exec will return an error and the error message will include the pipeline's output followed by error messages describing the abnormal terminations; the errorCode variable will contain additional information about the last abnormal termination encountered. If any of the commands writes to its standard error file and that standard error isn't redirected, then exec will return an error; the error message will include the pipeline's standard output, followed by messages about abnormal terminations (if any), followed by the standard error output."

And the there's this very handle code fragment from the Tcl'ers Wiki, specifically from
Wrap up your exec in a catch command, like so:

Code:
set status [catch {exec $theCommand $arg1 $arg2 ...} result]

Now you can find out with the combination of $status and $errorCode.

Code:
if { $status == 0 } {

  # The command succeeded, and wrote nothing to stderr.
  # $result contains what it wrote to stdout, unless you
  # redirected it

} else if { [string equal $::errorCode NONE] } {

  # The command exited with a normal status, but wrote something
  # to stderr, which is included in $result.

} else {

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

    CHILDKILLED {
      foreach { - pid sigName msg } $::errorCode break

      # A child process, whose process ID was $pid,
      # died on a signal named $sigName.  A human-
      # readable message appears in $msg.

    }

    CHILDSTATUS {

      foreach { - pid code } $::errorCode break

      # A child process, whose process ID was $pid,
      # exited with a non-zero exit status, $code.

    }

    CHILDSUSP {

      foreach { - pid sigName msg } $::errorCode break

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

    }

    POSIX {

      foreach { - errName msg } $::errorCode break

      # One of the kernel calls to launch the command
      # failed.  The error code is in $errName, and a
      # human-readable message is in $msg.

    }

  }
}
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top