Position independent Parameter passing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Position independent Parameter passing
# 1  
Old 09-10-2007
Position independent Parameter passing

Hi all,
When parameters are passed to the shell script, they are dereferenced by their position. For example,
I call myTest.sh and pass two parameters param1 and param2 as following:
./myTest.sh param1 param2

In the script, myTest.sh, I refer to first parameter (param1 ) as $1 and second parameter (param2) as $2.

Is there any way to make parameter passing independent. Something like
./myTest.sh -Dparameter1=param1 -Dparameter2=param2


Thanks,
Sonal.
# 2  
Old 09-10-2007
Perhaps this.

Code:
[/tmp]$ cat try.ksh
#! /bin/ksh
#

while [ $# -gt 0 ] 
do
    key=${1%=*}
    case $key in
        "-Dparameter1") echo "Param1=[${1#$key=}]" ;;
        "-Dparameter2") echo "Param2=[${1#$key=}]" ;;
    esac
    shift;
done
    
[/tmp]$ ./try.ksh -Dparameter1="something" -Dparameter2="more"
Param1=[something]
Param2=[more]
[/tmp]$ ./try.ksh -Dparameter2="more" -Dparameter1="something"
Param2=[more]
Param1=[something]
[/tmp]$

You can add more flags within the case-esac construct.
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

Passing parameter through file

Hi , I am passing date parameter through file my shell script testing.sh is #set -x #set -v asd=$1 asd1=$2 echo $asd echo $asd1 Passing parameter as below sh testing.sh `cat file1.txt` Output (2 Replies)
Discussion started by: kaushik02018
2 Replies

3. Shell Programming and Scripting

Passing parameter more than 9

Hi, I've written a script where eleven parameter to be passed from command line which is inserting into an oracle table, it is working but the tenth and 11th parameter are not accepting as given it is referring to 1st parameter. HERE IS THE SCRIPT #!/bin/ksh #set -o echo $*... (4 Replies)
Discussion started by: sankar
4 Replies

4. Shell Programming and Scripting

How to use this position parameter?

Hi Gurus, I want split one huge line file to multiple line, I got code from you guys yesterday. I want put the code in a script which use two position parameter when run the script. my code like: awk '{for (i=1; i<=length($0); i+=$2) print substr($0, i, $2)}' $1 > $1_split when I run the... (6 Replies)
Discussion started by: ken6503
6 Replies

5. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

6. Programming

passing float parameter

I am surprised by GCC (this is ver. 4.2.4, Ubuntu 32 bit Intel) when a function declares a float parameter and it's prototype is missing, the parameters are messed up. Please see my code below: ~/test$ cat x1.c #include <stdio.h> #include <stdlib.h> set_p(int p1, float p2, int p3, int p4)... (7 Replies)
Discussion started by: migurus
7 Replies

7. Shell Programming and Scripting

wrong parameter passing!

Hi all I have a script which will take input as filename and passes it to a java program. It is as follows -------------------------------- FILENAME=$1 echo $FILENAME ${JAVA_HOME}/bin/java -cp DateProvider $FILENAME ------------------------------------------------- when I execute the same... (2 Replies)
Discussion started by: malle
2 Replies

8. UNIX for Advanced & Expert Users

Parameter passing in a function

I need to pass a parameter to a function in a script. My parameter is a string. When I display the parameter within my function, I only get the first word from string I pass in. How can I make the function receive the whole string (and not terminate at the first space it encounters)?. part of... (2 Replies)
Discussion started by: fastgoon
2 Replies

9. Shell Programming and Scripting

parameter passing

Hallo everyone, This is my problem below: /home/cerebrus/pax=>vat class2.sh ksh: vat: not found /home/cerebrus/pax=>cat class2.sh #!/bin/ksh set -x bdf|grep appsdev|awk '{ print $5 }'> class3 dd={cat class3} echo $dd /home/cerebrus/pax=> /home/cerebrus/pax=>./class2.sh + bdf +... (8 Replies)
Discussion started by: kekanap
8 Replies

10. HP-UX

fd passing between Independent processes using unix domain sockets

Hi, I am having some error handling issues with and fd passed between Independent processes using unix domain sockets (On HPUX). Here is the scnerio ================= Step 1: TPC/Client (connect()) ---Connects to ------TCP/Server(Gateway) (server gets fd) Step 2: ... (2 Replies)
Discussion started by: Debasisb2002
2 Replies
Login or Register to Ask a Question