LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Issue escaping single quotes in bash function (https://www.linuxquestions.org/questions/linux-general-1/issue-escaping-single-quotes-in-bash-function-4175680347/)

rusorusich 08-13-2020 08:22 AM

Issue escaping single quotes in bash function
 
Hi,
I'm writing a bash script containing three functions: f_A, f_B and f_C.

1. f_A invoke f_B by passing it the string argument "Alpha".
2. f_B call f_C by passing it a partly dynamically constructed string.

My issue is this: I can't find the right syntax to build the string to pass to f_C.
In brief:

Quote:

f_A> --{ Alpha }--> f_B> --{ /hosts/host[name='Alpha'] }--> f_C
Calling function (f_B) is:

Code:

    f_B() {
        FQDN=$(echo "$1")    # $1 = Alpha
        APIPATH="/hosts/host[name='"${FQDN}"']"
        HOSTID="$(f_C 'hosts' $APIPATH)"
        echo "$HOSTID"
    }

APIPATH value must be equal to "/hosts/host[name='Alpha']"
Unfortunately I can't replace single quotes with double ones, the only accepted syntax is the one with single quotes

I've tried the following syntaxes with no luck:

Quote:

APIPATH="/hosts/host[name='${FQDN}']"

APIPATH=/hosts/host[name='${FQDN}']

APIPATH="/hosts/host[name='"'"'${FQDN}'"'"']"

APIPATH=$(eval echo "/hosts/host[name='${FQDN}']")

APIPATH=$(echo "/hosts/host[name='${FQDN}']")

APIPATH="/hosts/host[name=\'${FQDN}\']"

APIPATH="/hosts/host[name='"${FQDN}"']"
and so on...
I've never been able to get the desired string.

Thanks in advance to anyone who can help me.

pan64 08-14-2020 07:45 AM

hi and welcome here, at LQ

I would suggest you to user www.shellcheck.net to check your script, it will help you to fix issues like this.
Additionally you need to post your script, now we can't see the relevant parts therefore hard to tell you anything
You can also use set -xv at the beginning of your script to see what's happening.
Finally
Code:

FQDN=$(echo "$1")
# can be written as:
FQDN="$1"


rtmistler 08-14-2020 08:36 AM

What pan64 said, but I think there's a problem if you specifically have a file named like:
Code:

/hosts/host'some-name'
My point being is why in the resultant string would you require to have quotes? Because it does not appear to be a normally sensible hostname.

Perhaps you can print out some examples of: variable name is <blah>
Resultant string needs to be exactly <blah-blah> with all the syntax and punctuation as you feel it's needed to be.

rusorusich 08-14-2020 09:25 AM

Thanks for the replies to rtmistler and pan64. I especially appreciate the tip about shellcheck.net. The string of rtmistler's example doesn't represents a filename but is a string to pass out to an XML parser ("xpath" from "perl-XML-XPath" package), so I thought I needed to pass it to the parser with exactly defined quotes. I think I'm close to solving the problem with the help of shellcheck too. Probably "xpath" can accept the syntax
Code:

"/hosts/host[name='Alpha']"
in the same manner of
Code:

'/hosts/host[name="Alpha"]'
but I'm going to thoroughly test the solution. I think this thread can be marked as solved.

Ruso

MadeInGermany 08-14-2020 08:26 PM

Code:

    f_B() {
        FQDN=$1    # $1 = Alpha
        APIPATH="/hosts/host[name='$FQDN']"
        #or#APIPATH=/hosts/host[name=\'$FQDN\']
        HOSTID=$(f_C 'hosts' "$APIPATH")
        echo "$HOSTID"
    }

A var= assignment may have an unquoted $var or $( ) at the right side (because not further expansions will happen on them).
But a literal space or quote needs to be quoted.

Command arguments normally must have "" quotes around a $var or $( )
The function f_C is like a command so must have "$APIPATH".

rknichols 08-14-2020 10:10 PM

Quote:

Originally Posted by rusorusich (Post 6155484)
Calling function (f_B) is:

Code:

    f_B() {
        FQDN=$(echo "$1")    # $1 = Alpha
        APIPATH="/hosts/host[name='"${FQDN}"']"
        HOSTID="$(f_C 'hosts' $APIPATH)"
        echo "$HOSTID"
    }

APIPATH value must be equal to "/hosts/host[name='Alpha']"

Are you certain that your problem is with the value assigned to APIPATH? The above syntax works just fine for me.
Code:

[rkn] Desktop $ FQDN=Alpha
[rkn] Desktop $ APIPATH="/hosts/host[name='"${FQDN}"']"
[rkn] Desktop $ echo $APIPATH
/hosts/host[name='Alpha']

I would usually suggest using "set -x" to make the shell show what is happening at each step, but because of the way that single quotes get represented in the output, it could just add to the confusion.
Code:

[rkn] Desktop $ set -x
[rkn] Desktop $ APIPATH="/hosts/host[name='"${FQDN}"']"
+ APIPATH='/hosts/host[name='\''Alpha'\'']'
[rkn] Desktop $ echo $APIPATH
+ echo '/hosts/host[name='\''Alpha'\'']'
/hosts/host[name='Alpha']

Within a single-quoted string, the 4-character sequence ' \ ' ' is needed to represent a literal single quote.


All times are GMT -5. The time now is 03:47 PM.