Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2004
    Posts
    2

    Unanswered: 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

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    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.

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

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •