LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-13-2020, 08:22 AM   #1
rusorusich
LQ Newbie
 
Registered: May 2020
Posts: 2

Rep: Reputation: Disabled
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.
 
Old 08-14-2020, 07:45 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,786

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
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"
 
2 members found this post helpful.
Old 08-14-2020, 08:36 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,879
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
1 members found this post helpful.
Old 08-14-2020, 09:25 AM   #4
rusorusich
LQ Newbie
 
Registered: May 2020
Posts: 2

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-14-2020, 08:26 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,779

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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".
 
Old 08-14-2020, 10:10 PM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,774

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by rusorusich View Post
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.

Last edited by rknichols; 08-14-2020 at 10:20 PM. Reason: Add comments about "set -x"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Yet Another Bash Quotes Within Quotes Issue tboyer Linux - Software 17 11-03-2012 11:17 AM
translate value from single quotes to double quotes venkateshrupineni Linux - Newbie 2 06-14-2012 03:03 PM
escaping double quotes in a file koszta5 Linux - Newbie 3 12-12-2010 09:32 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 11:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration