Computing.Net > Forums > Unix > Capture exit codes from Unix

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Capture exit codes from Unix

Reply to Message Icon

Name: braveking
Date: May 14, 2004 at 18:44:41 Pacific
OS: solaris, AIX
CPU/Ram: 512
Comment:

Hi I have the following code that gets called from a main script

. $SCRIPT $DIVN | tee -a $LOGFILE

export ERR=$?
if (( $ERR != 0 ))
then
export EXVAL=$ERR
echo "\n$(date +"%D %T") Error: Process script $SCRIPT failed." | tee -a $LOGFILE
cat $LOGFILE > $HLDLOGFILE
exit $EXVAL
fi


Using the above code , even the $SCRIPT exits with return code > zero, the $? is not capturing it , because of the tee command.
I want to be able to write the messages from
$SCRIPT both to screen and logfile and at the same time manipulate the exit code.


I tried doing this
$SCRIPT $DIVN ; export ERR=$? | tee -a $LOGFILE
if (( $ERR != 0 )) ...etc

It still doesn't work. i am able to capture
ERR=1 from export, but when it comes to $ERR, it has a value of zero , maybe because my main script initialises it to zero at the beginning of main script.

Can some body help me to debug this?



Sponsored Link
Ads by Google

Response Number 1
Name: braveking
Date: May 16, 2004 at 11:42:45 Pacific
Reply:

By the way, no changes can be done to $SCRIPT. The main script is being created so that it can run any interface and takes care of the logging and maintains log files.


0

Response Number 2
Name: thepubba
Date: May 17, 2004 at 08:09:37 Pacific
Reply:

if you write a simple script like this:

#!/bin/ksh

ls -lai fdffds 2>/dev/null

The return value from the script will be 1 (since this file does not exist).

If you then rewrite the script to read:

#!/bin/ksh

ls -lai fdffds 2>/dev/null
errorCode=$?

The return value from the script will be 0. This is because the assignment of the value of $? to the variable errorCode was successful. If you then rewrite it script to read:

ls -lia fddfdsfds 2>/dev/null
errorCode=$?
if [[ $errorCode -ne 0 ]]
then
return $errorCode
fi

The return code from the script will be the value of $errorCode if it is not a 0.

So, what is probably happening in your script is that you are executing another command after you encounter the error. This command is successful so the script will return 0. You need to capture the error at the point of the occurance and explictly return it or you will continue to get a 0 return code.

Jerry



0

Response Number 3
Name: braveking
Date: May 17, 2004 at 09:32:29 Pacific
Reply:

Hi Jerry..Thx for responding. I understand your reasoning. My question is How do i capture the error code ? At the same time I want to be able to display the error messages and log the error messages from the sub script.


0

Response Number 4
Name: Jim Boothe
Date: May 17, 2004 at 11:56:49 Pacific
Reply:

Capturing error code within a series of piped commands has always been a problem for me also. One solution is to avoid the pipe.  Redirect stdout to a file, which can then be sent to the screen and the logfile:

tempout=/tmp/myscript$$
$SCRIPT $DIVN > $tempout
err=$?
cat $tempout | tee -a logfile
rm $tempout
if [ $err -ne 0 ] ...


0

Response Number 5
Name: nails
Date: May 17, 2004 at 13:50:41 Pacific
Reply:

Hi:

Jim and Jerry have some good ideas - especially about capturing the exit code of piped commands. Depending on program $SCRIPT, have you thought about checking stderr?

SCRIPT=who
cmd="$SCRIPT 2> err.file |tee -a log.file"

If $SCRIPT is a well designed program, if there any errors, then should be in the standard error file - err.file in this case.

Regards,

Nails


0

Related Posts

See More



Response Number 6
Name: nails
Date: May 17, 2004 at 13:52:16 Pacific
Reply:

Whoops!

Forgot the eval:

SCRIPT=w
cmd="$SCRIPT 2> err.file |tee -a log.file"

eval $cmd



0

Response Number 7
Name: Dlonra
Date: May 17, 2004 at 14:17:07 Pacific
Reply:

TMTOWTDI
my preference:
(. $SCRIPT $DIVN ; echo $? >RES)| tee -a $LOGFILE
export ERR=$(cat RES)


0

Response Number 8
Name: shahnawaz
Date: June 4, 2004 at 13:18:16 Pacific
Reply:

Hi

Try trap command. The syntax is

trap ' command ' ERR


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Capture exit codes from Unix

Urgent HELP. capture exit code Perl www.computing.net/answers/unix/urgent-help-capture-exit-code-perl/6367.html

Capturing the signals from UNIX www.computing.net/answers/unix/capturing-the-signals-from-unix-/2710.html

Can't compile Stevens code from apu www.computing.net/answers/unix/cant-compile-stevens-code-from-apu/5931.html