Capture Exit Codes from Unix

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.
 

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.
 

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.
 

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.
 

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

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.
 

Whoops!
Forgot the eval:

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

eval $cmd
 

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

Try trap command. The syntax is

trap ' command ' ERR

Have a Unix Problem
Unix Forum - Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.