Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Passing variable as an argument to another script

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 09-13-2013
ahamed101's Unix or Linux Image
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 12 October 2015, 2:40 PM EDT
Location: San Jose, CA
Posts: 1,910
Thanks: 54
Thanked 488 Times in 481 Posts
Try

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

--ahamed
Sponsored Links
    #3  
Old Unix and Linux 09-13-2013
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 24 August 2016, 5:58 AM EDT
Posts: 104
Thanks: 0
Thanked 11 Times in 11 Posts
Try this:

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

Andrew
    #4  
Old Unix and Linux 09-13-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 8 August 2016, 12:07 PM EDT
Posts: 1,650
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 Unix and Linux 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 Unix and Linux 09-13-2013
ahamed101's Unix or Linux Image
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 12 October 2015, 2:40 PM EDT
Location: San Jose, CA
Posts: 1,910
Thanks: 54
Thanked 488 Times in 481 Posts
What is this problem being caused in the python script? Is it not able to parse it?

--ahamed
Sponsored Links
    #7  
Old Unix and Linux 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:
Passing variable as an argument to another script 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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 09:56 AM.