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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exit code from previous command

Status
Not open for further replies.

KSiva

Programmer
Feb 18, 2002
63
AU
Hi all,

How do I get the exit code from previous execute command. For examples

in TCL,

set r [catch {exec shellCommandFile} ]

in shellCommandFile, I have exit code like

#!/bin/sh
exit 60

How do I get that exit code ( 60 in this case ), from TCL program.

I am not talking about the errorCode but the exit code. Can some one up there to help me!

:)) Siva
 
It isn't the most obvious of places to look for it, but it's stored in the global variable errorCode. To quote from the (tclvars) reference page:

"After an error has occurred, this variable will be set to hold additional information about the error in a form that is easy to process with programs. errorCode consists of a Tcl list with one or more elements. The first element of the list identifies a general class of errors, and determines the format of the rest of the list."

When a child process exits with a non-zero exit code (which causes exec to raise an error condition), errorCode will contain a 3-element list:

"CHILDSTATUS pid code

This format is used when a child process has exited with a non-zero exit status. The second element of errorCode will be the process's identifier (in decimal) and the third element will be the exit code returned by the process (also in decimal)." - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 

That's excellent ! That helps me a lot!

Another innocent question ... how do I extract the 3 rd element which is exit code? Is there any built in function to do that ? Or Do i need to use regsub to get rid of first 2 elements and get the 3 rd one?

Thanks you!


----------------------
Another question ,

in shell script I execute a command like

. myProgram arg1

Note the " . " infront.

When I tried that in tcl ,

exec . myProgram arg1

which turns into an error. How do I excute the similar command in tcl ....?


------------------
P.S : Right now I am converting a huge application from shell script to tcl and I am newbie to tcl programming. Where Can i get the help for equivalant code of shell -> tcl .... Any help ?
-------------------
 
First Question:
set mycode [lindex $errorCode 2]

Second Question:
try either using
#not in path
eval `type -p tclsh scriptname`
#in path
tclsh scriptname
or:
placing the path of your tcl interpreter on the
first line of your script:
example
#!/usr/bin/tclsh.
There is one other way, but it's a kludge.

Third question:
How huge is huge?
Tcl is better , yeah, but not for everything.
Some things the shell and unix tools are easier
to use, especially in a *nix environment.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top