Call single function multiple times diff set of parameters

Okay, not sure if it can be done, I would think it could be done and I'm just having a hard time with it.

fun_close_wait(){
ipVar="${1} ${2}"
portVar=`cat "${5}" | cut -d' ' -f 1`
for ip in $ipVar
do
        for port in $portVar
        do

                netstatVar=`netstat -n | grep CLOSE_WAIT | grep "${ip}:${port}" | wc -l`
                        if [[ -n "${ip}" && ${ip} == "$1" ]]
                                then
                                        echo `hostname`-"${3}"-`cat "${5}" | grep "${port}" | cut -d' ' -f 2`-${netstatVar}
                                        elif [[ -n "${ip}" && ${ip} == "$2" ]]
                                                then
                                                echo `hostname`-"${4}"-`cat "${5}" | grep "${port}" | cut -d' ' -f 2`-${netstatVar}
                                        else
                                                echo `hostname`-"${ip}"-`cat "${5}" | grep "${port}" | cut -d' ' -f 2`-${netstatVar}

                        fi
        done
done
exit
}
fun_close_wait 11.11.111.11 22.22.222.22 server1 server2 first-port-list-file
fun_close_wait 33.33.333.33 44.44.444.44 server 3 server4 second-port-list-file

Right now, it seems to run through the first function, but once it gets to the second function it doesn't run through it. To test it I commented out the first function call and it runs just fine, so both function call's work and the script does what it's intended to do, only when I try to call the function twice is where I get the issue.

in the port list files it's just a flat file like the following:

1111 instance1
2222 instance2
3333 instance3
4444 instance4
5555 instance5

I haven't traced through this, but if this is a copy and paste:

fun_close_wait 11.11.111.11 22.22.222.22 server1 server2 first-port-list-file
fun_close_wait 33.33.333.33 44.44.444.44 server 3 server4 second-port-list-file

then those two calls don't have the same number of arguments. It looks like "server 3" should be "server3" in the second line.

Looks like you may have the same problem as the guy here: getopts not updating from subroutine.

You have an exit command at the end of your function so I don't see how you'd even be able to run though it twice but that's neither here nor there. Try adding the following line:

OPTIND=1

between your final "done" and "exit"

That's a typo when I took out the actual IP's and server names. In the actual code they have the same number of arguments.

---------- Post updated at 08:28 AM ---------- Previous update was at 08:26 AM ----------

Taking the exit out and putting it after the function calls worked.

I originally created this w/o it being a function, and decided it'd be easier for me to use multiple function calls inside a single script, rather than passing multiple parameters from outside, and forgot to take out the exit line.

exit will exit the script even it's in function. If you need to make "exit" in function = interrupt function, then use return in function like you use exit.

function some
{
    [ "$#" -lt 2 ] && return 1
    do_something
    # you can say return, but it has done automatic also
}

#main

# calling function = calling command = calling alias = ...
some arg1
$stat=$?   # look exit code of some, in this case function return code
echo "some exit status $stat"