ksh: quoting in a $( ... ) block

ksh: quoting in a $( ... ) block

Post by Jurjen Oska » Sun, 07 Dec 2003 18:12:05



Hi there,

I've got a question about ksh and the use of quoting in a $( .. ) block:

hithere="just testing"

select foo
from bar
where baz='${hithere}';
exit;
EOF ) || exit 1

What happens in this case is that sqlplus complains because it
sees double quotes in its input, instead of single quotes. If I replace
the $( ... ) with backticks ` it works fine.

Am I doing Something Ugly(tm) here? I prefer to use a $( ... ) block,
but how can I get the quoting right?

Thanks,
--
Jurjen Oskam

PGP Key available at http://www.stupendous.org/

 
 
 

ksh: quoting in a $( ... ) block

Post by Michael Pao » Mon, 08 Dec 2003 13:53:29



> I've got a question about ksh and the use of quoting in a $( .. ) block:
> hithere="just testing"

> select foo
> from bar
> where baz='${hithere}';
> exit;
> EOF ) || exit 1
     ^
> What happens in this case is that sqlplus complains because it
> sees double quotes in its input, instead of single quotes. If I replace
> the $( ... ) with backticks ` it works fine.
> Am I doing Something Ugly(tm) here? I prefer to use a $( ... ) block,
> but how can I get the quoting right?

I think it most likely the issue you're encountering is due to the space
between EOF and ).  You might also be encountering some additional
quoting issues.

Compare what occurs when I feed these different shell segments to ksh
(at least the version I have handy).  If I use bash, I also get similar
results if there's no space between the EOF and closing ) or ` (and with
a space in there, results are slightly different, but also other than
what the code would imply was intended).

ksh input:
hithere="just testing"
output=$(cat <<EOF
baz='${hithere}';
EOF )
echo "$? $output"

ksh output (including stderr):
ksh: here document `EOF' unclosed

ksh input:
hithere="just testing"
output=$(cat <<EOF
baz='${hithere}';
EOF)
echo "$? $output"

ksh output (including stderr):
0 baz='just testing';

ksh input:
hithere="just testing"
output=`cat <<EOF
baz='${hithere}';
EOF `
echo "$? $output"

ksh output (including stderr):
ksh: here document `EOF' unclosed

ksh input:
hithere="just testing"
output=`cat <<EOF
baz='${hithere}';
EOF`
echo "$? $output"

ksh output (including stderr):
0 baz='just testing';

Note also that within here document, only command substitution,
parameter substitution and backslash quoting is done - all else is
passed literally.  (These substitutions are suppressed if any part of
marker is quoted, and if <<- is used instead of <<, leading tabs are
stripped).

So, if you have shell script including:

hithere="just testing"

select foo
from bar
where baz='${hithere}';
exit;
EOF ) || exit 1


ignore precisely how and where your here document is terminated or if
it's missing termination) is:
select foo
from bar
where baz='just testing';
exit;

e.g. just testing gets substituted in place of ${hithere} and the rest

|       << marker
|              after reading the command line containing this kind
|              of redirection (called a here document), the  shell
|              copies  lines from the command source into a tempo-
|              rary file until a line  matching  marker  is  read.
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...

ksh(1)
Caveat: your ksh mileage may vary.

 
 
 

ksh: quoting in a $( ... ) block

Post by Jurjen Oska » Mon, 08 Dec 2003 20:55:24



> ksh output (including stderr):
> ksh: here document `EOF' unclosed

I should have included the real code instead of typing from memory, sorry.
In the real code, there was nothing between the EOF and the ` or ).

Quote:> ksh input:
> hithere="just testing"
> output=$(cat <<EOF
> baz='${hithere}';
> EOF)
> echo "$? $output"

> ksh output (including stderr):
> 0 baz='just testing';

Hmmm... The following script:

#!/bin/ksh
USER="joskam"
output=`cat << EOF
baz='${USER}';
EOF`
print -- "${?}:${output}"

...produces the following output:

0:baz='joskam';

If I change the script to:

#!/bin/ksh
USER="joskam"
output=$(cat << EOF
baz='${USER}';
EOF)
print -- "${?}:${output}"

... the output changes to:

0:baz="${USER}";

Which is obviously not what you're seeing.

Quote:> Caveat: your ksh mileage may vary.

It appears so. May I ask what version of the Korn shell you're using? I'm
using "Version M-11/16/88f" from AIX 4.3.3 (bos.rte.shell 4.3.3.80), and
"Version M-11/16/88f" from AIX 5.2 (bos.rte.shell 5.2.0.14).

AIX 5.2 also comes with ksh93 (Version M-12/28/93e). Given the following
script, named try3.sh:

#!/bin/false
USER="joskam"
output=$(cat << EOF
baz='${USER}';
EOF)
print -- "${?}:${output}"

... gives the following results:

$ echo ${.sh.version}
Version M-12/28/93e
$ . ./try3.sh
ksh93: .: syntax error: `(' unmatched.

$ exit

$ temp/ksh93       /* the "real" ksh93 binary downloaded from kornshell.com */
$ echo ${.sh.version}
Version M 1993-12-28 o+
$ . ./try3.sh
0:
$

The following script, named try4.sh:

#!/bin/false
USER="joskam"
output=`cat << EOF
baz='${USER}';
EOF`
print -- "${?}:${output}"

... gives the following results:

$ echo ${.sh.version}
Version M-12/28/93e
$ . ./try4.sh
0:
$ exit

$ temp/ksh93
$ echo ${.sh.version}
Version M 1993-12-28 o+
$ . ./try4.sh
0:
$

I don't know why in ksh93 it doesn't seem to work at all...

--
Jurjen Oskam

PGP Key available at http://www.stupendous.org/

 
 
 

1. What's 'side effects' of Ksh built-ins?

Hi,

I have a book called "The new KornShell Command and Programming
Language". In several chapters, it mentions "side effects" of
built-ins. I'm confused what's that so that I can't understand some
paragraphs. Any help is appreciated.

Lesley Yang

2. Enlightenment locks my system!!

3. sh/ksh/bash quoting brain tease

4. Installing for the first time

5. quoting in sh/ksh

6. cdrecord

7. ksh Quoting and IFS Question

8. mytec Raid-controller don′t find disks

9. Quoting in KSH

10. passing quoted string to perl from ksh

11. Problem with sh/ksh quoting

12. ksh - regular expressions and quoting problem in command

13. Urgent: double quote in ksh