Passing variable as an argument to another script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Passing variable as an argument to another script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-13-2013
Manasa Pradeep Manasa Pradeep is offline
Registered User
 
Join Date: May 2013
Last Activity: 13 September 2013, 7:56 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Passing variable as an argument to another script

Hi,

I am trying to pass a variable as an argument to another script. While substitution of variable, I am facing a problem.
varaiable "a" value should be -b "FPT MAIN".
When we pass "a" to another script, we are expecing it to get substitue as ./test.sh -b "FPT MAIN". But, it is getting substituted as ./test.sh -b '"FPT' 'MAIN"', which is causing a problem.

Could someone suggest how to achieve this.



Code:
bash$ a=" -b \"FPT MAIN\" "; 
bash$ echo $a
-b "FPT MAIN"
bash$
bash$
bash$ a=" -b \"FPT MAIN\" "; echo $a ;set -x ; ./test.sh $a; set +x
-b "FPT MAIN"
+ ./test.sh -b '"FPT' 'MAIN"'
-b "FPT MAIN"
+ set +x
bash$
bash$
bash$ cat test.sh
#!/bin/bash
a=$@
echo $a

Sponsored Links
    #2  
Old 09-13-2013
ahamed101's Avatar
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 15 May 2014, 2:54 PM EDT
Location: San Jose, CA
Posts: 1,906
Thanks: 54
Thanked 485 Times in 478 Posts
Try

Code:
a=" -b \"FPT MAIN\" "; echo $a ;set -x ; ./test.sh "$a"; set +x

--ahamed
Sponsored Links
    #3  
Old 09-13-2013
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 29 July 2014, 8:57 AM EDT
Posts: 98
Thanks: 0
Thanked 10 Times in 10 Posts
Try this:

Code:
a=" -b \"FPT MAIN\" "; echo $a ;set -x ; eval ./test.sh $a; set +x

Andrew
    #4  
Old 09-13-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
Quote:
Originally Posted by Manasa Pradeep View Post
Hi,

I am trying to pass a variable as an argument to another script. While substitution of variable, I am facing a problem.
varaiable "a" value should be -b "FPT MAIN".
When we pass "a" to another script, we are expecing it to get substitue as ./test.sh -b "FPT MAIN". But, it is getting substituted as ./test.sh -b '"FPT' 'MAIN"', which is causing a problem.

Could someone suggest how to achieve this.



Code:
bash$ a=" -b \"FPT MAIN\" "; 
bash$ echo $a
-b "FPT MAIN"
bash$
bash$
bash$ a=" -b \"FPT MAIN\" "; echo $a ;set -x ; ./test.sh $a; set +x
-b "FPT MAIN"  # First output
+ ./test.sh -b '"FPT' 'MAIN"' # Why this causing problem? This is just showing how system handles it.
-b "FPT MAIN" # Second output - same as fist output. What is the difference.? 
+ set +x
bash$
bash$
bash$ cat test.sh
#!/bin/bash
a=$@
echo $a

Sponsored Links
    #5  
Old 09-13-2013
Manasa Pradeep Manasa Pradeep is offline
Registered User
 
Join Date: May 2013
Last Activity: 13 September 2013, 7:56 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the reply.

But with the solution, getting additional single codes ./test.sh ' -b "FPT MAIN" '. This single quote is creating problem.


Code:
bash$ a=" -b \"FPT MAIN\" "; echo $a ;set -x ; ./test.sh "$a"; set +x
-b "FPT MAIN"
+ ./test.sh ' -b "FPT MAIN" '
-b "FPT MAIN"
+ set +x
bash$

---------- Post updated at 03:03 AM ---------- Previous update was at 03:01 AM ----------

This is just an example I have put.

Actually, I am passing $a to some python script, which needs argument in format -b "FPT2 Main" -e 1node_ran_ref -v 371145 -R

Last edited by Franklin52; 09-13-2013 at 07:33 AM.. Reason: Please use code tags
Sponsored Links
    #6  
Old 09-13-2013
ahamed101's Avatar
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 15 May 2014, 2:54 PM EDT
Location: San Jose, CA
Posts: 1,906
Thanks: 54
Thanked 485 Times in 478 Posts
What is this problem being caused in the python script? Is it not able to parse it?

--ahamed
Sponsored Links
    #7  
Old 09-13-2013
Manasa Pradeep Manasa Pradeep is offline
Registered User
 
Join Date: May 2013
Last Activity: 13 September 2013, 7:56 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Yep.
bcoz of single quote, ' -b "FPT MAIN" '
it is not able to recognise arguments.



Code:
a="-b \"FPT2 Main\" -e 1node_ran_ref -v 371145 -R" ; echo $a ; set -x ;/build/fpsdkroot_extra/bin/cicli.py getlastCompleteOKbuild "$a" ; set +x
-b "FPT2 Main" -e 1node_ran_ref -v 371145 -R
+ /build/fpsdkroot_extra/bin/cicli.py getlastCompleteOKbuild '-b "FPT2 Main" -e 1node_ran_ref -v 371145 -R'
NO SUCH BRANCH
ERROR: 11: Branch object not found
+ set +x

-bash-3.00$ /build/fpsdkroot_extra/bin/cicli.py getlastCompleteOKbuild  -b "FPT2 Main" -e 1node_ran_ref -v 371145 -R
1node_ran_ref:
R_FPT_2.4.1.1.WR.64.rh.1309031601.371145
DEPLOYMENT:
create_1node_ran_ref.sh

-bash-3.00$

Moderator's Comments:
Please use CODE tags (not ICODE tags) for multi-line code segments.

Last edited by Don Cragun; 09-13-2013 at 04:43 AM.. Reason: Change ICODE tags to CODE tags.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing awk variable argument to a script which is being called inside awk vivek d r Shell Programming and Scripting 2 02-11-2013 06:37 AM
Passing full path as argument when it contains variable strings KidD312 Programming 1 06-27-2012 05:28 AM
Passing full path as argument when it contains variable strings KidD312 UNIX for Advanced & Expert Users 0 06-25-2012 01:21 PM
Passing argument from one script to other lloydnwo Shell Programming and Scripting 3 10-27-2005 02:06 PM
Passing the command line argument in a variable rkumar28 Shell Programming and Scripting 1 05-26-2004 03:02 PM



All times are GMT -4. The time now is 01:11 AM.