Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 )) ...etcIt 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?

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
fiThe 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

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.

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

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

![]() |
![]() |
![]() |

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