Re: Passing ampersands through a shell script
Re: Passing ampersands through a shell script
- Subject: Re: Passing ampersands through a shell script
- From: Bruce Brown <email@hidden>
- Date: Sun, 25 Oct 2009 10:54:39 -0700
Looks like you've solved your immediate problem, so I'll offer the
following for thought, further discussion, and future reference ...
You could also QUOTE the ampersand before passing it to the Unix
shell. Quoting a single character in Unix (i.e., in the Shell) is
done by preceding that character with a backslash (\) character. So,
to quote an ampersand, you'd replace the single character
&
with the two-character string
\&
in an Unix command or in an Unix script.
The backslash removes any special interpretation by the shell of the
immediately-following character. So, that character (an ampersand, in
this situation) is treated by the shell as just another ASCII
character. The backslash character itself is "swallowed"--that is, it
gets stripped off by the Shell, as it's reading and interpreting the
command. So, it has no other effect on things beyond removing special
interpretation of the ampersand.
In your first Applescript code snippet, you'd have to somehow scan
the text in the variable "trackArtist" for the presence of an
ampersand, and insert a backslash character just before each
ampersand you found there.
You could scan the variable from left to right or from right to left,
but remember to keep scanning all the way to the end of the line;
don't stop after finding only the first ampersand. There could be
more than one ampersand in the same text returned from iTunes.
You'd also want to repeat this same scanning procedure on your other
variables, too, to ensure there are no unquoted ampersands in them,
coming in from iTunes, that eventually get passed to Unix as part of
an Unix command.
Further, there could be additional characters (other than ampersands)
in the text from iTunes that have special meaning to the Shell. To be
safe, instead of quoting only ampersand characters, you could
probably quote ANY character that's not in the set of characters
[0-9A-Za-z_]. In other words, numeric digits, upper- and lowercase
letters, and the underscore character should be "shell-safe."
Possibly, the space character would be shell-safe, too--but, that
would probably depend on how your Applescript code and your shell
code are written.
As an aside, normally, at a shell command prompt, the ampersand
character at the END of a command line tells the shell to run the
command "in the background." That, in turn (I think) causes the shell
to create a sub-shell, then run your command in that sub-shell. When
the command terminates (i.e., when it finishes running), the sub-
shell also goes away automatically.
Running an Unix command in the background also causes the shell
prompt to return immediately (that is, if you're running the shell
interactively, such as in Terminal, not in a shell script), while the
command you entered continues to run (in the sub-shell).
I'm not sure, however, what the Shell does with any stray characters
that follow an ampersand character in a shell command. It might put
up an error, or it might try to interpret them as a second shell
command. Or, possibly, it merely discards them. However, as a
programmer, I wouldn't want to rely on something like that. Better to
pass a "clean" command to the shell, than to rely on relatively
obscure "quirks" of the shell, such as discarding characters
following an ampersand (if that's, in fact, what the shell does).
Remember, some day, that shell might change to a different one, and
the stray characters might not be handled the same way by that shell.
Apple has changed which Unix shell is the default shell in Darwin (in
Unix) in OSX at least twice now, since the first version of OSX came
out ca. 1999-2000.
-B.
On Oct 25, 2009, at 2:13 AM, LuKreme wrote:
On 25-Oct-2009, at 03:04, hd wrote:
That's done it! (Changing --data-binary to --data-urlencode.)
BTW, I've combined this script (Which I've saved as an Application
named TweetTunes) with TweetMymac so that when I send 'open
TweetTunes' to my TweetMymac twitter ID, it posts the currently
playing song.
http://themacbox.co.uk/tweetmymac/
--
I got a question. If you guys know so much about women, how come
you're here at like the Gas 'n' Sip on a Saturday night
completely alone drinking beers with no women anywhere?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (applescript-
email@hidden)
Help/Unsubscribe/Update your Subscription:
40mac.com
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden
_______________________________________________
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