rob@xxxxxxxxx wrote:
This works:
echo $1
mycmd='CPYFRMIMPF FROMSTMF(''"'${1}'"'') TOFILE(ROB/TESTSH) RCDDLM(*CRLF)'
echo $mycmd
system $mycmd
Here's a simpler syntax:
mycmd="CPYFRMIMPF FROMSTMF('$1') TOFILE(ROB/TESTSH) RCDDLM(*CRLF)"
echo "$mycmd"
system "$mycmd"
Want to know why yours worked? Let's examine it...
Whenever you put two strings in quotes next to each other, the system
concatenates the strings. So if I might have this:
> echo "QSYS"
QSYS
$
> echo '.LIB'
.LIB
$
> echo "QSYS"'.LIB'
QSYS.LIB
$
See... in that last example, I got QSYS.LIB because it concatenated the
two strings, one containing QSYS the other containing .LIB. They were
concatenated together.
Another important point... when you are in the middle of a string
delimited by double quotes, the single quote character is treated as a
normal character, and vice versa. So you could do this:
> echo "Klement's Sausage"
Klement's Sausage
$
> echo 'Scott "Darkbagel" Klement'
Scott "Darkbagel" Klement
$
See what I mean? The quotes aren't treated as delimiters when their
already inside another set of quotes. They just act like normal characters.
however... unlike RPG or CL, when you put two quotes consecutively, it
does NOT equal a single quote. Instead, it just figures you want to
concatenate two strings.
If you're used to RPG or CL, you might expect this to work:
> echo 'Klement''s Sausage'
Klements Sausage
$
It didn't work. It didn't insert the apostrophe, because that's not how
shells view things. It sees this as two strings, 'Klement' and
's Sausage' that are to be concatenated together. In fact, the
preceding QShell would be more akin to the following CL code:
'Klement' *CAT 's Sausage'
Hope that makes sense... two consecutive quotes does not insert a
quote, it just causes the two strings to be concatenated.
Now let's look at your example:
'CPYFRMIMPF FROMSTMF(''"'${1}'"'') TOFILE(ROB/TESTSH) RCDDLM(*CRLF)'
QShell sees this as a set of strings to be concatenated. It'll have
broken them down like this:
1- 'CPYFRMIMPF FROMSTMF('
2- '"'
3- ${1}
4- '"'
5- ') TOFILE(ROB/TESTSH) RCDDLM(*CRLF)'
Then concatenate them together, which will work. However, you'll notice
that ${1} is not inside any quotes... that means that this will only
work with the "system" utility if ${1} has no spaces in it. Probably
good enough for this program, though.
It's also more complicated than it needs to be. There was no need for
the '"' to be in a separate string from the 'CPYFRMIMPF'. Since you
were already inside single quotes, the double quotes didn't need any
special escaping. You could've accomplished the same thing like this:
'CPYFRMIMPF FROMSTMF("'${1}'") TOFILE(ROB/TESTSH) RCDDLM(*CRLF)'
That does precisely the same thing that your example did.. the other
quotes were superfluous, and frankly made it slightly harder to read.
But, the ${1} still can't have any spaces in it. Plus, you are passing
double quotes to the "system" utility instead of single quotes, which,
IMHO, just looks wrong :) There's really no need for your string to be
inside strong quotes! Inside double-quotes, variables will be expanded,
so you no longer have to put ${1} in a separate string. Plus, you can
use single quotes instead of double quotes to surround the pathname.
"CPYFRMIMPF FROMSTMF('${1}') TOFILE(ROB/TESTSH) RCDDLM(*CRLF)"
Then... there's no reason to use ${} -- since the $1 would be surrounded
by the single quotes, it won't confuse it with another variable. So you
can leave off the curly braces (which can be a boon for globalization)
"CPYFRMIMPF FROMSTMF('$1') TOFILE(ROB/TESTSH) RCDDLM(*CRLF)"
So that's the "simpler" version I arrived at. IMHO, it's cleaner to
read and easier to understand. Plus, since the whole thing is still
wrapped up in the double quotes, there's no problem having a space
inside the stream file name. For example, if the stream file were named
'/home/klemscot/maint quote.txt' this code would still work properly.
Sorry... long winded, I know... but I thought you might want to learn a
little more about why yours worked, and how it could be simplified.
As an Amazon Associate we earn from qualifying purchases.