Call single function multiple times diff set of parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Call single function multiple times diff set of parameters
# 1  
Old 08-04-2009
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.

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

Code:
1111 instance1
2222 instance2
3333 instance3
4444 instance4
5555 instance5

# 2  
Old 08-04-2009
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.
# 3  
Old 08-04-2009
Looks like you may have the same problem as the guy here: https://www.unix.com/shell-programmin...ubroutine.html

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:

Code:
OPTIND=1

between your final "done" and "exit"
# 4  
Old 08-05-2009
Quote:
Originally Posted by kodak
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.
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 ----------

Quote:
Originally Posted by mglenney
Looks like you may have the same problem as the guy here: https://www.unix.com/shell-programmin...ubroutine.html

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:

Code:
OPTIND=1

between your final "done" and "exit"
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.
# 5  
Old 08-05-2009
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.
Code:
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"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to call Oracle function with multiple arguments from shell script?

Dear All, I want to know how can i call oracle function from shell script code . My oracle function have around 5 input parameters and one return value. for name in *.csv; do echo "connecting to DB and start processing '$name' file at " echo "csv file name=$x" sqlplus -s scoot/tiger <!... (2 Replies)
Discussion started by: Balraj
2 Replies

2. Shell Programming and Scripting

How to call a bash script with positional parameters?

Hi, I have a script which will be executed using the below command, bin/nutch crawl urls -dir /data/test/ bin/nutch - Script file crawl, urls, /data/test/ - Parameters -dir - Option The above script should executed from a shell script named test.sh. I have the below code to execute... (2 Replies)
Discussion started by: vel4ever
2 Replies

3. UNIX for Advanced & Expert Users

System call failed with 127 .. after 500 times when called in loop

Hi Experts, I have a code like this. ===== #include.... int main() { int count = 0; while(1){ printf("\n Interation number is: %d \n ",count); rv = system(" test.sh > log.txt " ); if (-1 == rv) { printf("Could not generate static log: error... (12 Replies)
Discussion started by: binnyjeshan
12 Replies

4. Programming

Problem with implementing the times() function in C (struct tms times return zero/negative values)

Hello, i'm trying to implement the times() function and i'm programming in C. I'm using the "struct tms" structure which consists of the fields: The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process. The tms_stime structure... (1 Reply)
Discussion started by: g_p
1 Replies

5. Shell Programming and Scripting

To call a Oracle sql file 1000 times

Hi Guys, Even though, i have called db2 sql file earlier, this is my first time to call a oracle sql file. I need to make a database(oracle) connection and then call the sql file in a loop. Can you please help me out. Thanks for your help and time. Regards, Magesh (4 Replies)
Discussion started by: mac4rfree
4 Replies

6. Shell Programming and Scripting

call function with different parameters position

Hi I would like to call function in my script couple of times. But each time with different parameters position. In line 57 I don't know how to call it without typing $1 and $ parameters ? #!/bin/ksh 2 3 4 function First 5 { 6 7 # $1 - name 8 ... (2 Replies)
Discussion started by: presul
2 Replies

7. Shell Programming and Scripting

call a passwd file to a script multiple times

Hello everybody, I have a requirement in my script.. When i'am executing a script, it'll ask a passwd of some service account.. I need to pass it to the script through a zipped file when it asks for it. The script can be executed by more people many number times. So for securty purpose, it... (1 Reply)
Discussion started by: raghu.iv85
1 Replies

8. Infrastructure Monitoring

diffrence between method call and function call in perl

Hello, I have a problem with package and name space. require "/Mehran/DSGateEngineLib/general.pl"; use strict; sub System_Status_Main_Service_Status_Intrusion_Prevention { my %idpstatus; my @result; &General_ReadHash("/var/dsg/idp/settings",\%idpstatus); #print... (4 Replies)
Discussion started by: Zaxon
4 Replies

9. UNIX for Dummies Questions & Answers

what is diff b/w wait3() and wait4() system call

helo, i read man pages but still confuse about wait3() and wait4() stystem call what is exact diff b/w wait3() and wait4() system call and what is diff b/w waitpid() and wait3() and wait4() Regards, Amit (1 Reply)
Discussion started by: amitpansuria
1 Replies

10. UNIX for Dummies Questions & Answers

Call a UNIX shell with parameters from C

Hello...I hava quite a problem, couldn't find a solution anywhere :(. I have a C program, and from that C program I have to call a shell script. This is not difficult, I can do it using the "system" command from C. But the ugly part is how can I send as parameters some variables? For example...i... (1 Reply)
Discussion started by: dustman
1 Replies
Login or Register to Ask a Question