call function with different parameters position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting call function with different parameters position
# 1  
Old 06-19-2010
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 ?

Code:
 #!/bin/ksh
     2
     3
     4  function First
     5  {
     6
     7  # $1 - name
     8  # $2 - second name
     9  # $3 - age
    10  # $4 - sex
    11
    12          typeset -r MY_NAME=$1
    13          typeset -r MY_2nd_NAME=$2
    14          typeset -ri AGE=$3
    15          typeset -r SEX=$4
    16
    17
    18  if [[ -n $MY_NAME && -n $MY_2nd_NAME ]]       
    19  then
    20
    21          print -n "Name is: $MY_NAME in function locally"
    22          print
    23
    24          print -n "Second name is: $MY_2nd_NAME in function locally"
    25          print
    26  else
    27          print "My age is: $AGE"
    28          print
    29          
    30          print "My sex is: $SEX"
    31          print
    32  fi
    33  }
    34
    35
    36
    37
    38  # Start of the program
    39
    40  print -n "Podaj imie: "
    41  read NAME
    42  print
    43
    44  print -n "Podaj nazwisko: "  
    45  read nd_NAME 
    46  print
    47
    48
    49  print "Running local funtion First"
    50  First $NAME $nd_NAME    
    51  print
    52
    53
    54 print "Calling function First again, but now with other parameters"
    55  print
    56
    57  First $1 $2 29 male


thx for help
# 2  
Old 06-19-2010
You use probably script in below

Code:
./myscript Name Surname

and print this information..
so what do you want exactly?

you can call script also this like without pos parameters
Code:
First Name Surname 29 male

and script
Code:
./myscript

# 3  
Old 06-19-2010
I want to call function First() with its positional parameters $1 $2 and so on but don't want those parameters to be positional parameters from script file.

Don't know how to substitute $1, $2 when I call this function, cause I want to only call this First() with parameters $3 and $4.

---------- Post updated at 11:23 PM ---------- Previous update was at 11:17 PM ----------

I got it Smilie
Solution in line 57.
I just have called the function and paste in positional parameters $1 and $2 no values at all.


Code:
    57  First "" "" "29" "male"

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 pass position parameter into function.?

Hi Gurus, I have request which needs to pass position parameter to a function. I tried below simple code, it doesn't work. #!/bin/bash func_1(){ echo $1 } func_1 $ ./set_file abc $ do I need add some to get the position para first? thanks in advance. (3 Replies)
Discussion started by: ken6503
3 Replies

2. Shell Programming and Scripting

[Solved] Array for parameters from 5th position

Hi All, I am writing a script where the first 5 parameters are mandatory but the script should be able to handle a maximum of 9 parameters (with the remainig 4 optional) I would like to load all parameters from 5th parameter positioninto an array. the piece of code I am writing for this:... (0 Replies)
Discussion started by: snailrider
0 Replies

3. 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

4. Shell Programming and Scripting

After exit from function it should not call other function

Below is my script that is function properly per my conditions but I am facing one problem here that is when one function fails then Iy should not check other functions but it calls the other function too So anyone can help me how could i achieve this? iNOUT i AM GIVING TO THE... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

5. Shell Programming and Scripting

Passing the parameters using a function

Hi All, I am new to shell scripting required some help in passing the parameter value to the shell script. I am writing a shell script, in the script I have created two functions as below. first function get_trend_ids () { Here I am connecting to the database and getting all the... (3 Replies)
Discussion started by: shruthidwh
3 Replies

6. Shell Programming and Scripting

Pass parameters to function

Hi, for example I have this function: function get_param () { test=echo "some string" test2=echo "someother string" } I want to call this function and get test or test2 result, how do I do that ? Thank you (2 Replies)
Discussion started by: ktm
2 Replies

7. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: cbo0485
4 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. Shell Programming and Scripting

call shell script with named parameters

Could someone help me to call a shell script with named parameters. I mean Callscript .sh -c someconfigfile.txt or callscript.sh -config someconfigfile.txt inside the script how can I retrieve someconfigfile.txt using the parametername -c or -config. Thanks. (2 Replies)
Discussion started by: rvijay80
2 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