quoting $ in the Csh

Mike Taylor mirk at warwick.UUCP
Tue Dec 13 23:55:09 AEST 1988


In article <1799 at solo9.cs.vu.nl> maart at cs.vu.nl (Maarten Litmaath) writes:
>Csh's syntax + parser are SICK! Csh is an example of `loving your enemy' :-)

How right you are - csh is beautiful for interactive use, but a real pain
when it comes to working out tricky quotations.  Maybe best of all is when
you get yourself a nice little one-liner awk-script, and try to make that
into an alias.  Cos of course, the trouble is, you have to protect your
true meaning in such a way that not only do you quote all your meta-
characters, but that you also quote the characters that quote them in such
a way that they will be able to continue to do so after the initial layer
of quoting has been stripped off during the "alias" command itself!  (Phew!)

This is generally OK if your expression has no single-quotes in it, in which
case you can just wrap it all up in single-quotes.  If it _does_ have any,
then you hit big trouble, because as our on-line csh(1) manual accurately
points out in its BUGS section:

	Quoting conventions and contradictory and confusing.

A simple example?  OK.
Suppose you want to know how many news articles you have:  Simple.
Generate a list of the first 999 newsgroups (should be enough! :-) together
with a count of the articles in each, using "rn -c -s999".  Then feed the
result to awk '{x+=$5}; END {print x}', which prints the sum of the 5th
field of each line.

Now try to build and alias for it?

	alias cart 'rn -c -s999 | awk '{x+=$5}; END {print x}''

Thjis won't work because csh(1) gets confused about which single quotes
belong to the "alias" statement and which belong to "awk".  (What an
awful explaination.  Never mind.  You know what I mean).

So maybe we try:

	alias cart 'rn -c -s999 | awk \'{x+=$5}; END {print x}\''

No deal.  I can't say exactly what is going wrong here, but for some reason,
(Any of you Toreks/Spencers out there like to explain) csh(1) decides that
quoting the inner 's with \ isn't good enough, and spits back a "Unmatched
single quote" error at you.

So try using double quotes to surround the alias - better, but not good enough,
because remember there is no way to prevent variable substitution taking place
within double quotes.  To cut a long story short(er), here is what I ended
up with, ensuring that the $ is not enclosed in double quotes, but in
single ones:

	alias cart "rn -c -s999 | awk '{x+="'$5'"}; END {print x}'"

Cruddy, huh?  There must be a better way to do these things.
Ah well, until that day ...
______________________________________________________________________________
Mike Taylor - {Christ,M{athemat,us}ic}ian ...  Email to: mirk at uk.ac.warwick.cs
*** Unkle Mirk sez: "Gm F#7 Bb Cm->Gm->Eb->Gm F; Gm F#7 Bb C Eb C Bb Dm D" ***
------------------------------------------------------------------------------



More information about the Comp.unix.questions mailing list