Re: Escape Apostrophes in 'Do Shell Script'
Re: Escape Apostrophes in 'Do Shell Script'
- Subject: Re: Escape Apostrophes in 'Do Shell Script'
- From: Christopher Nebel <email@hidden>
- Date: Thu, 6 Nov 2003 10:44:02 -0800
On Nov 5, 2003, at 10:27 PM, Emmanuel wrote:
At 11:01 PM -0600 05/11/03, Jay Young wrote:
Basically, I'm wondering if there's a way to escape apostrophes in a
'do shell script' in case a word contains an apostrophe itself.
I think that Christopher Nebel is sleeping, so:
---------------------------
quoted form of "What's Up?"
--> "'What'\\''s Up?'"
---------------------------
Yes, I was. I'm up now. (Yawn. Stretch.)
The usual answer to this sort of thing is "use the 'quoted form'
property", but the original question is actually more complicated than
that. You wrote:
do shell script "perl -e 'print \"What's Up?\";'"
Notice that you've put an apostrophe inside a single quoted string.
This is trouble, because as soon as the shell sees the apostrophe, it
thinks it should end the single quoted string, and then sees the rest
of the line as uninterpretable garbage. To fix this, you need to
emulate what you'd have to do in the shell, which is this:
perl -e 'print "What'\''s Up?";'
Or, in AppleScript with the extra backslashes:
do shell script "perl -e 'print \"What'\\''s Up?\";'"
It takes a bit of staring to get this, but what's happening is that the
first ' opens the string, the second one closes it, then there's a
literal ' (thanks to the backslash), and then another ' to re-open the
string, and finally one more ' to close it again. Because there are no
spaces, the shell mushes all of those together into one string
argument. Not coincidentally, the '\'' sequence is how "quoted form"
escapes single quotes in a string.
It's not clear to me how to solve this problem in a general way.
(Actually, I can think of several ways, but they involve either
different approaches or a bunch of extra code.) "quoted form" is
designed to do shell-style quoting, which does not work in Perl.
Obviously, you could mangle the string yourself, but that's work.
--Chris Nebel
AppleScript Engineering
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.