1

Some context :

I'm attempting to run a command as part of a bash script to poll for system/device data.

# Recommended usage in the command line, contains single and double quotes
wbemcli ein 'http://localhost:5988/root/cimv2:some_class'
wbemcli gi 'http://localhost:5988/root/cimv2:some_class.DeviceID="Some Device ID"'

# I can get away with just 1 level of quotation marks
wbemcli ein http://localhost:5988/root/cimv2:some_class
wbemcli gi http://localhost:5988/root/cimv2:some_class.DeviceID="Some Device ID"
wbemcli gi http://localhost:5988/root/cimv2:some_class.DeviceID='Some Device ID'

So this is working ...

    #!/bin/sh

    C_PATH=http://localhost:5988/root/cimv2
    CLASS=some_class
    ID="Some Device ID"

    set -x
    wbemcli ein $C_PATH:$CLASS

Unfortunately, it falls apart when I attempt to integrate the quotation marks into the command. The code executed in the shell is unexpected to me.

    # Code
    wbemcli gi $C_PATH:$CLASS.DeviceID=\"$ID\"

    output $ ./myscript
    + wbemcli gi 'http://localhost:5988/root/cimv2:some_class.DeviceID="Some' Device 'ID"'

    # I was expecting this ...
    output $ ./myscript
    + wbemcli gi http://localhost:5988/root/cimv2:some_class.DeviceID="Some Device ID"




Bash is adding quotation marks in places I didn't expect. It's even enclosing the entire URL portion in single quotation marks. What's going on ?

1 Answer 1

1

Try like this:

wbemcli gi -nl "$C_PATH:$CLASS.DeviceID=\"$ID\""

or like this:

wbemcli gi -nl "$C_PATH:$CLASS.DeviceID='$ID'"
Sign up to request clarification or add additional context in comments.

6 Comments

1st one gives + wbemcli gi -nl 'C_PATH:some_class.DeviceID="Some Device ID"' and 2nd one gives + wbemcli gi -nl 'C_PATH:some_class.DeviceID='\''Some Device ID'\'''
@peonicles: Both the traces are OK — the wbemcli command gets to see either single quotes or double quotes around the 'Some Device ID' and sees the whole string as a single argument even though there are spaces in 'Some Device ID'. bash does echo things differently under set -x from other shells; its output is typically unambiguous where other shells' output is ambiguous.
ahh ok, I had a really dumb syntax error (omitted the $ in $C_PATH). #1 is working perfectly, thanks guys! #2 is still adding the weird '\' though.
That's just how bash displays the string when you have set -x enabled. It's the string C_PATH:some_class.DeviceID=, followed by a literal single quote \', the string Some Device ID, another literal single quote \' and the empty string ''. You get this weird-looking representation, because nested single quotes can't be escaped in single-quoted strings.
I've substituted in actual values into the variables, and #1 is working nicely. #2 seems to give me a usable string when I echo, but for some reason wbemcli is giving me errors. Your answer should work for other similar situations though.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.