return code of read and/or for

I am trying to validate an ip address, based on what is in the netmasks file,

I have a function that checks the values of the ip against the network and netmask, this returns a 1 if the ip doesn't match and a 0 if the IP does.

#####################
#Validate IP Network#
#####################

validate_ip_network()
{
        typeset ip4addr="$1"
        typeset network="$2"
        typeset netmask="$3"

        old_IFS=$IFS
        IFS=.

declare -a IP4
IP4=($ip4addr)

declare -i ip4_1=${IP4[0]}
declare -i ip4_2=${IP4[1]}
declare -i ip4_3=${IP4[2]}
declare -i ip4_4=${IP4[3]}

declare -a NET
NET=($netmask)
declare -i nm_1=${NET[0]}
declare -i nm_2=${NET[1]}
declare -i nm_3=${NET[2]}
declare -i nm_4=${NET[3]}

declare -i MASK=255

# check network value entered  is numeric #
        xx=0
        echo $ip4_4 | egrep "^[0-9]+$" > /dev/null
        xx=`expr $xx + $?`
        echo $ip4_3 | egrep "^[0-9]+$" > /dev/null
        xx=`expr $xx + $?`
        echo $ip4_2 | egrep "^[0-9]+$" > /dev/null
        xx=`expr $xx + $?`
        echo $ip4_1 | egrep "^[0-9]+$" > /dev/null
        xx=`expr $xx + $?`
        if [ $xx -gt 0 ]; then
           echo "non numeric network value entered"
           return 1
        fi

        calculated_network="$(( ip4_1 & nm_1 )).$(( ip4_2 & nm_2 )).$(( ip4_3 & nm_3 )).$(( ip4_4 & nm_4 ))"
        if [[ "$network" = "$calculated_network" ]] ; then

                # calculate broadcast address
                _BROADCAST="$(( ip4_1 | (MASK ^ nm_1 ))).$(( ip4_2 | (MASK ^ nm_2 ))).$(( ip4_3 | (MASK ^ nm_3 ))).$(( ip4_4 | (MASK ^ nm_4 )))"

                IFS=$old_IFS
                return 0
        else
                IFS=$old_IFS
                return 1
        fi

}

This gets called by another function that reads through the netmasks file to get the network id and netmasks, originally I had used a read, but this always returns a 0 (see the hashed out section). I then changed this to a for loop, but I am unsure how to exit out of this with a return 1 I want the loop to continue for all the values in the netmasks file, but if no match the overall function has to exit a 1,

validate_ip ()
{
        typeset ip=$1

old_IFS=$IFS
IFS=$'\n'

for line in $(/usr/xpg4/bin/grep -E "^[0-9]" /etc/netmasks)
do
        network=`$ECHO $line | $AWK -F" " '{print $1}'`
        netmask=`$ECHO $line | $AWK -F" " '{print $2}'`
        validate_ip_network $1 $network $netmask
        if [ $? -eq 0 ];
        then
                $ECHO "Network Exists"
                break
        else

        fi

done

IFS=$old_IFS


#         /usr/xpg4/bin/grep -E "^[0-9]" /etc/netmasks | while read network netmask ; do
#                if validate_ip_network $1 $network $netmask ; then
#                        return 0
#                fi
#        done
}

Any tips greatly appreciated

Set a retval variable to the exit code you want, start by setting it to ok

retval=0

if the loop detects an error:

       retval=$?
       if [ $retval -eq 0 ];
        then
                $ECHO "Network Exists"
                break
        else
           # exit $retval     
               $ECHO "Network does not exist"
             break
        fi
#  last line
 return retval
# or if the end of the script
 exit $retval