Re: perl regex works differently within a "do shell script" than from within perl
Re: perl regex works differently within a "do shell script" than from within perl
- Subject: Re: perl regex works differently within a "do shell script" than from within perl
- From: Steve Hayman <email@hidden>
- Date: Fri, 27 Apr 2007 15:51:05 -0400
However, when I put it in an Applescript like so:
set fixedName to do shell script "echo " & stringFromTheClipboard &
"| perl -pe s/^\\s+//g;"
fixedName gives me a result of
/Volume s/et cetera/
with trailing spaces also removed, and multiple spaces condensed
down to one.
That's because you aren't quoting stringFromTheClipboard. So the
shell itself is stripping off a lot of the spaces before echo and
perl even get to it. Shells parse a command line into individual
arguments, by removing spaces, before handing the arguments off to a
program like echo. In your case, the shell is parsing
echo /Volume s/et ceter/ | perl -pe s/^\s+//g;
in such a way that the echo program gets
argument 1: "/Volume"
argument 2: "s/et"
argument 3: "ceter/"
which the echo command helpfully outputs with a single space between
the options, so what perl is getting is the string "/Volume s/et
ceter/".
You can fix this with "quoted form of", e.g. try
set fixedName to do shell script "echo " & quoted form of
stringFromTheClipboard & "| perl -pe s/^\\s+//g;"
which does
echo ' /Volume s/et ceter/ ' | perl -pe s/^\s+//g;
thus preserving that string as a single unparsed option to be passed
to echo as is..
Personally I'd quote the perl expression too because it might contain
shell metacharacters (it doesn't in your example, but many of them
do.) Ultimately I would do
set fixedName to do shell script "echo " & quoted form of
stringFromTheClipboard & "| perl -pe 's/^\\s+//g;'"
hope this helps
steve
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden