Returning exit code from a while read $inputline

Hi searched hi and wide for this with no luck.

Maintaining a bash script that

 
#!/usr/bin/bash
#does some stuff like setting env etc.
f_do_dbwork
 
...
..
 
#Now I want to exit with the value of $err however $err is re-initialised to 0 on exiting the function
Always 0
....
 
f_do_dbwork() {
echo "$err" #- Always 0
#does a datbase call to set file that feeds the inputline command
cat $TEMP_FILE | while read inputline
do
#set variables
#call another shell script which returns an exit code of 0 or -6
 
$err=$?
if [ $err -ne 0 ] ; then 
   exit $err
fi
#more stuff
done
}

Don't have time to rewrite the cat $TEMP_FILE | while read inputline bit.

How do I get a status from the function back to the calling part of main shell script?

It has to do with Bash running the while loop in a sub-shell. It's a known "problem".

You can search for "Bash while loop variable", for more info.

Thanks for the quick response I'll go do some research.

Quick fix:
Exit the function with return $err (instead of exit $err ) and test $? in the main script immediately after calling f_do_dbwork