J
john.d.russell
I have a problem that ultimately seems to be a shell problem, but since
I could work around it in the shell but not in Perl, this seems like
the most likely group.
Let's say I've got a file with a single quote in its name, e.g.
Foo'Bar. I want to include this filename inside a command invoked
through backticks.
print `ls -l 'Foo\'Bar'`;
This produces the error:
sh: -c: line 1: unexpected EOF while looking for matching `''
(Which is extra confusing because it looks like the message is trying
to emulate curly quotes by enclosing the looked-for single-quote
character with ` on one side and ' on the other.)
Doing away with backticks doesn't work. system() gives the same error:
print system("ls -l 'Foo\'Bar'");
The same thing happens when issuing that exact command in the shell,
but there I'm free to swap in double quotes. In the actual scenario
that I've boiled down to this one-liner, I'm constructing a more
elaborate string literal and interpolating a variable in a certain
spot, and that variable's value might or might not have a single quote
embedded in it. So I'm looking for some way to escape the variable's
value so that it can be substituted without triggering this error when
the resulting string is passed to the shell.
I'm running on OS X 10.4, with Perl 5.8.6. I've verified that both
pdksh and bash give the same error when issuing the command there.
I've searched the web and this group, but haven't found an example or
discussion of this same combination of quoting, backticking, and
escaping.
Thanks,
John
I could work around it in the shell but not in Perl, this seems like
the most likely group.
Let's say I've got a file with a single quote in its name, e.g.
Foo'Bar. I want to include this filename inside a command invoked
through backticks.
print `ls -l 'Foo\'Bar'`;
This produces the error:
sh: -c: line 1: unexpected EOF while looking for matching `''
(Which is extra confusing because it looks like the message is trying
to emulate curly quotes by enclosing the looked-for single-quote
character with ` on one side and ' on the other.)
Doing away with backticks doesn't work. system() gives the same error:
print system("ls -l 'Foo\'Bar'");
The same thing happens when issuing that exact command in the shell,
but there I'm free to swap in double quotes. In the actual scenario
that I've boiled down to this one-liner, I'm constructing a more
elaborate string literal and interpolating a variable in a certain
spot, and that variable's value might or might not have a single quote
embedded in it. So I'm looking for some way to escape the variable's
value so that it can be substituted without triggering this error when
the resulting string is passed to the shell.
I'm running on OS X 10.4, with Perl 5.8.6. I've verified that both
pdksh and bash give the same error when issuing the command there.
I've searched the web and this group, but haven't found an example or
discussion of this same combination of quoting, backticking, and
escaping.
Thanks,
John