Passing parameter filled variable to a script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing parameter filled variable to a script.
# 1  
Old 10-13-2013
Passing parameter filled variable to a script.

Say I have something like this to try and build a parameter string:

Code:
OPT1="This is my Option";
OPT2="/folder/file";

PARMS="-u me -J \"$OPT1\" -g \"$OPT2\"";

./script.ksh -n 1 $PARMS;
./script.ksh -n 2 $PARMS;
./script.ksh -n 3 $PARMS;
./script.ksh -n 4 $PARMS;

But this doesn't work. For example, it just sees "This" passed instead of the whole string. Parameters without spaces seem fine. Is there anyway to do so? Tried ' and { and combinations of but no luck. Can't seem to pass the whole string in. Here's how it looks (' "This ' is printed but not the whole string):

Code:
# ./dummy.ksh
NARG=|1|
UARG=|me|
GARG=||
JARG=|"This|

NARG=|2|
UARG=|me|
GARG=||
JARG=|"This|

NARG=|3|
UARG=|me|
GARG=||
JARG=|"This|

NARG=|4|
UARG=|me|
GARG=||
JARG=|"This|

# cat script.ksh
#!/usr/bin/ksh


while getopts n:u:g:J: option; do
        case ${option} in
                n)      NARG="$OPTARG"
                        ;;
                u)      UARG="$OPTARG"
                        ;;
                g)      GARG="$OPTARG"
                        ;;
                J)      JARG="$OPTARG"
                        ;;
                [?])    print >&2 "Good Luck!";
                        exit 1
                        ;;
                esac
done


print "NARG=|$NARG|";
print "UARG=|$UARG|";
print "GARG=|$GARG|";
print "JARG=|$JARG|";
print;
# cat dummy.ksh
#!/usr/bin/ksh

OPT1="This is my Option";
OPT2="/folder/file";

PARMS="-u me -J \"$OPT1\" -g \"$OPT2\"";

./script.ksh -n 1 $PARMS;
./script.ksh -n 2 $PARMS;
./script.ksh -n 3 $PARMS;
./script.ksh -n 4 $PARMS;

#

Passing directly works fine, but not through a parameter of flags.

Cheers,
DH

Last edited by Devyn; 10-13-2013 at 10:12 PM..
# 2  
Old 10-14-2013
I does not work like you expect, because the expansion of $PARAMS will use $IFS (space, tab, newline by default) to separate fields and thus it will ignore double quotes.

So
Code:
./script.ksh -n 1 -u me -J "$OPT1" -g "$OPT2"

should work.

To put that in a variable first and then expand it later, you could use something like eval but then there may be security concerns ...

Last edited by Scrutinizer; 10-14-2013 at 09:06 AM..
# 3  
Old 10-14-2013
You can also have a look at an exemple here
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing output parameter(Oracle) to variable in ksh Script

Hello, I have researched and tried many way to pass OUT parameter to be stored in variable in KSH Script.Still not success, please help. Here is my Store Procedure. create procedure testout3(v_in varchar2,v_out OUT integer) as begin v_out := 1; end; Here is my testvout.ksh #!/bin/ksh... (1 Reply)
Discussion started by: palita2601
1 Replies

2. UNIX for Dummies Questions & Answers

Passing shell script parameter value to awk command in side the script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff |... (1 Reply)
Discussion started by: Sarita Behera
1 Replies

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

4. Shell Programming and Scripting

Passing Parameter containing space in between to Shell Script

Hi, I have one shell script which use two parameter however one of its parameter have space in between. eg. a.sh 20110114 b c d here b c d is one parameter I used 'b c d' but its not giving correct result. Also i tried b\c\d but this one also didnt work. Any help would be... (5 Replies)
Discussion started by: diehard
5 Replies

5. AIX

Passing a parameter to a shell script?

I would like to run a compress script on files in certain directories. My compress_script.sh is just basically compress file* In order for me to use this I have to copy it into each directory and run it. How can I state the directory on the command line with the compress script so it... (2 Replies)
Discussion started by: NycUnxer
2 Replies

6. Shell Programming and Scripting

Issue in passing a parameter value from one script to another

Hi Experts, I am passing current month & day (i.e. 'Jul 21') from script aaa.ksh to zzz.ksh. The value 'Mon DD' is being used in zzz.ksh. Problem: If I pass 'Mon DD' value manually to zzz.ksh i.e. /test/zzz.ksh 'Jul 21' it works fine. However, if I pass value from aaa.ksh, it does... (2 Replies)
Discussion started by: dipeshvshah
2 Replies

7. Shell Programming and Scripting

Passing parameter from one file to shell script

Hi All, I have a 2 files. File1 i am generating using an ETL tool, which is a comman seperated delimited file which contains country code & load date. everytime, this country code will be updated from a table. It might be AB or BA & ld_date will be for which date we need to load the file. ... (7 Replies)
Discussion started by: Amit.Sagpariya
7 Replies

8. Shell Programming and Scripting

Passing Filename as a Parameter to Another script

Hi, I have a requirement to write a script file(script1.sh) to read the file from the folder say /usr1/profiles/active and pass the file name as a parameter to another script file say (script2.sh) which would subsitute this parameter. The script2.sh will have a line like the one below ... (1 Reply)
Discussion started by: ravisg
1 Replies

9. Shell Programming and Scripting

Passing a parameter while calling a script

Hello everyone I was asked to pass a parameter in the calling of a script. IŽll have two parameters London and Birmingham and IŽll need to pass this because each of these will have a specific script however with the same name. /home/script/loaddata.ksh Lond /home/script/loaddata.ksh Birm... (2 Replies)
Discussion started by: Rafael.Buria
2 Replies

10. Shell Programming and Scripting

Passing Date as parameter with sh script

Hi I need to pass a date argument with my shell script. Can any one send me code for the same. eg For 15th Aug 2006. Iwant to pass parameter like this ./startenv.sh 08/15/2006. Everyday date argument must change. Will the below code work ? ./startenv.sh date '+%m/%d/%y' THanks... (1 Reply)
Discussion started by: pankajkrmishra
1 Replies
Login or Register to Ask a Question