return code of read and/or for


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting return code of read and/or for
# 1  
Old 05-10-2012
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.

Code:
#####################
#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,

Code:
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
# 2  
Old 05-11-2012
Set a retval variable to the exit code you want, start by setting it to ok
Code:
retval=0

if the loop detects an error:
Code:
       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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Better to Use Return Code or wc -l Output?

Hey All, Quick question... I'm writing a short script to check if a continuous port is running on a server. I'm using "ps -ef | grep -v grep | grep processName" and I was wondering if it was better/more reliable to just check the return code from the command or if its better to pipe to... (12 Replies)
Discussion started by: mrm5102
12 Replies

2. Shell Programming and Scripting

How could I use the value of return code

Hello, I am woring on a script where I am getting strange situation.This script actually fetch the source code and tar that code and send to NAS location.This code resides in MKS tool...and we are fetching the source code on checkpoint label basis and script is working fine.First it synch the... (0 Replies)
Discussion started by: anuragpgtgerman
0 Replies

3. Shell Programming and Scripting

Unix return code example

Hi, Does anyone here can guide me to understand how is return code works in a parent-child relation with a simple example? I have a request to build the script with return code in a child script, but i want to understand how does child script can return a code to the parent, stated if its... (4 Replies)
Discussion started by: khchong
4 Replies

4. Shell Programming and Scripting

calling sqlplus, read table return etc

I have korn shell scripts. I want to pass a variable to a script which will execute a a sql script to read a table that contains env. variables. I want to read and then somehow export at unix level variables example for every row selected from the table build export command line field1... (5 Replies)
Discussion started by: TimHortons
5 Replies

5. Shell Programming and Scripting

return code help

Hello folks, I have a question that if i type ls command and type echo $? it always show "0", how i could do this change that when i type ls it will show me 1, actually i want to change the return code of commands from 0 to 1. Thanks Bash (5 Replies)
Discussion started by: learnbash
5 Replies

6. Shell Programming and Scripting

Need help with return code 1...

Hi Guys,, I am having a unix script which is running the DB2 Insert command. For the insert command, there were no records to be updated. SQL0100W No row was found for FETCH, UPDATE or DELETE; or the result of a query is an empty table. SQLSTATE=02000 + + echo 1 STAGE_RC=1 + ] ... (6 Replies)
Discussion started by: mac4rfree
6 Replies

7. Shell Programming and Scripting

Read Hostname and Return IP Address

Dear Experts, I have a text file on my itanium box which contains list of nearly 1000 hostnames. I do not have the IP Address of them. I want read each entry from the text file and want to do a nslookup to the DNS Server and get the IP Adresses and put them in another file in the format... (1 Reply)
Discussion started by: PrasannaKS
1 Replies

8. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

9. Shell Programming and Scripting

asking about return code

hi all my system is linux red hat i have a script that runs some object . the object return some code to the system i see the code by writing echo $? i want to ask in the script if $? equals 14 how shell is do that in the script thanks (3 Replies)
Discussion started by: naamas03
3 Replies

10. UNIX for Advanced & Expert Users

Return code from PL/SQL Code

Hi Guys, I was just wondering if anybody can help me with this problem. OK, how we can get a value back from PL/SQL Script (not stored procedure/function) See the below example: (for example aaa.sh) #!/bin/ksh VALUE=`sqlplus -s user/password@test_id <<EOF @xxx.sq EOF` echo $VALUE ... (7 Replies)
Discussion started by: Shaz
7 Replies
Login or Register to Ask a Question