exit status from the script is always 0

Hi ,

I have a bash script , which does the network configuration. Messages from this script are dumped on console as well as stored in a log file .

This script is invoked from a C code using system call . The script returns different exit code , to indicate different error cases. The pseudo-code of the script is below

#!/bin/sh

{
.
.
.
# If some error case 1
exit 2
.
.
.
# If some error case 2
exit 4

#script ends here
} 2>&1 |tee -a $LOG_FILENAME

Now the problem is that due to this "pipe tee" to re-direct the messages to log file , the exit status of this script is always 0.

Can some one give some suggestions , so that I can get the correct exit code of this script in my C program , as well as the messages are dumped on console as well as is logged on in a log file .

Why use tee? If you want the content to write in a file only use >>
If you want to look at it tee way, you only have to use tail -f $filename

I would start by removing or commenting out the last line with tee to see if that is the culprit...
test without and see what is returned (but it looks obvious the pipe is responsible...).