The problem you have is down to differences in how CL escapes quotes and
how Bourne Shells escape quotes.
 
[1]https://en.wikipedia.org/wiki/String_literal#Delimiter_collision
 
In CL, all strings use single quotes. In Bourne Shell, you can use either
single *or* double quotes.
 
In CL, to insert a single quote in a string, you double it up to escape
it: call MYPGM 'this: '' is a quote'
 
In Bourne shell, you can use single quotes inside double quotes just fine.
You can use double quotes inside single quotes just fine. It is impossible
to put single quotes inside single quoted strings, but you can put double
quotes inside double quoted strings by using a backslash before the
embedde double quote to escape it (similar to C/C++/Java/etc): echo "this:
\" is a double quote"
So you would need to do two levels of escaping to deal with the fact that
you're calling the string through QSH through CL and have to deal with
both of their differences. Not fun, but doable.
[2]https://en.wikipedia.org/wiki/String_literal#Nested_escaping
 
Nested escaping can get pretty hairy and nigh-unreadable:
[3]https://en.wikipedia.org/wiki/Nested_quotation#Nested_strings_(level_3_and_beyond)

----- Original message -----
From: John Yeung <gallium.arsenide@xxxxxxxxx>
Sent by: "OpenSource" <opensource-bounces@xxxxxxxxxxxxxxxxxx>
To: IBMi Open Source Roundtable <opensource@xxxxxxxxxxxxxxxxxx>
Cc:
Subject: [EXTERNAL] [IBMiOSS] Quotes in parameters (to QSH)
Date: Thu, Feb 6, 2020 1:23 PM
 
This was going to be a question, but then it turned into a "today I
learned...", and finally is a question again, but a harder one. I'm
including the TIL in the beginning, with the new question at the end.

My problem: I was trying to figure out how to include a literal
single-quote (a.k.a. apostrophe) in a parameter that I was passing to
a Python script that I was calling via QSH.

I know about the escape-by-doubling rule, so I thought it would be:

  qsh cmd('python3 script.py ''with space'' '''with''''quote''')

To make it easier to read, if you're not copying and pasting, let's
pretend ^ is the single-quote character. Then my attempt was:

  qsh cmd(^python3 script.py ^^with space^^ ^^with^^^^quote^^^)

The literal space in the first parameter came through fine. But the
second parameter actually became

  withquote

That is, the (re)doubled quotes completely canceled themselves out and
went away. I tried quite many things, including some that involved
backslashes, and at my wits' end I found this, which asks about spaces
rather than quotes:

  [4]https://www.ibm.com/developerworks/community/forums/html/topic?id=7e8784cc-ad3e-4a1e-b125-1e534191d344

Yeah, it's on dW, with a big banner at the top that says it's
available for read-only until the end of March, at which point it will
be completely gone. I'd rather not regurgitate the entire contents
here because my post is too long as it is (sorry, future archive
readers!).

The answer was provided by Barbara Morris. In short, she says to
simply use double-quotes as parameter delimiters! So in my case, it
would be

  qsh cmd('python3 script.py "with space" "with''quote"')

Note that the literal single-quotes are now doubled instead of
quadrupled. Again, for easier reading, let's pretend ^ is
single-quote, and we'll use # for double-quote:

  qsh cmd(^python3 script.py #with space# #with^^quote#^)

All is great, except Barbara points out, for completeness, that she
doesn't know what to do if you want a literal double-quote. Well, my
earlier attempt shows that you can use single-quotes as your parameter
delimiters, so you just do this:

  qsh cmd('python3 script.py "with space" ''with"dblquote''')

For better readability:

  qsh cmd(^python3 script.py #with space# ^^with#dblquote^^^)

So it seems that you can use either single- or double-quotes as
parameter delimiters, whichever is more convenient. (In this case, it
will probably be double-quotes, because they are just one character
each inside the single-quotes that delimit the entire command.)

But now, what about the (rare but possible) case where you want to
pass a string that contains both a single-quote and a double-quote?
Are we just out of luck?

(I'm guessing yes.)

John Y.
--
This is the IBMi Open Source Roundtable (OpenSource) mailing list
To post a message email: OpenSource@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: [5]https://lists.midrange.com/mailman/listinfo/opensource ;
or email: OpenSource-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at [6]https://archive.midrange.com/opensource ;.

Help support midrange.com by shopping at amazon.com with our affiliate
link: [7]https://amazon.midrange.com ;
 

 

References

Visible links
1. https://en.wikipedia.org/wiki/String_literal#Delimiter_collision
2. https://en.wikipedia.org/wiki/String_literal#Nested_escaping
3. https://en.wikipedia.org/wiki/Nested_quotation#Nested_strings_(level_3_and_beyond
4. https://www.ibm.com/developerworks/community/forums/html/topic?id=7e8784cc-ad3e-4a1e-b125-1e534191d344
5. https://lists.midrange.com/mailman/listinfo/opensource
6. https://archive.midrange.com/opensource
7. https://amazon.midrange.com/

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2022 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.