If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > variable substitution

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-02-04, 14:49
rkling01 rkling01 is offline
Registered User
 
Join Date: Mar 2004
Posts: 2
variable substitution

Hello,

I'm having a problem with passing the value of a variable into a command line parameter that requires the command to be passed between single quotes.

This works fine...

stccmd -cmd 'status test_component' |egrep -e 'Element |Hostname |State|Last Update Time' > $a_component_status_file #get status

and when I turn verbose on I see this ..WITHOUT the single quotes displayed (like I said this works)

+ egrep -e Element |Hostname |State|Last Update Time
+ stccmd -cmd status test_component
+ 1> /tmp/stc_15619_.tmp


but when I try to pass the component name as a variable

component_name="test_component"

stccmd -cmd \'status $component_name\' |egrep -e 'Element |Hostname |State|Last Update Time' > $a_component_status_file #get status

+ egrep -e Element |Hostname |State|Last Update Time
+ stccmd -cmd 'status test_component'
+ 1> /tmp/stc_15619_.tmp

and when I turn verbose on I see this ..WITH the single quotes displayed (like I said this DOES NOT work)


It looks like it should work, the variable substitution seems to take place but the command doesn't execute. I've tried so many iterations that I'm totally stumped.

Would anyone be so kind as to offer any thoughts?

Thanks,

Randy
Reply With Quote
  #2 (permalink)  
Old 03-02-04, 15:29
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Use double quote instead of signle :
Code:
stccmd -cmd "status $component_name" | \
egrep -e 'Element |Hostname |State|Last Update Time' > $a_component_status_file #get status
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 03-02-04, 17:13
rkling01 rkling01 is offline
Registered User
 
Join Date: Mar 2004
Posts: 2
That worked! Thank-you so much.

Now to understand why....

It appears the double quote is preserving the particular command I'm trying to execute which expects the -cmd with the command passed in single quotes when you execute it on a command line. By passing this in double quotes in the script is having the same effect as the single quoting does at the prompt. Of course the single quote disables referencing the variable contents in the script. Maybe there is a better explaination...but thanks again!
Reply With Quote
  #4 (permalink)  
Old 03-03-04, 03:16
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Single and double quotes delimit a parameter :
without quotes : a b => 2 parameters 'a' and 'b'
with quotes : 'a b' => 1 parameter 'a b'

When you use double quotes, the makes variable and escaped characters (\n\t..) substitutions within quotes; that is not the case with single quotes.

var=VALUE
echo 'var = $var' => var = $var
echo "var = $var" => var = VALUE
__________________
Jean-Pierre.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On