csh question -- aliasing, quoting and the like

Rouben Rostamian rostamia at umbc5.umbc.edu
Wed Sep 20 14:23:27 AEST 1989


In article <498 at westc.UUCP> gjoost at westc.UUCP (Gertjan van Oosten) writes:
>The basic problem is as follows: I want an alias to kill all processes
>called 'roy'.
>
>% alias killroy 'kill `ps ax | grep -w roy | grep -v grep | awk '{print $1}'`'
>
>The problem comes down to having single quotes within single quotes. The
>single quotes around the 'awk' argument can't be double quotes, because they
>would lead to the evaluation of '$1'. Escaping the '$' is no use within
>double quotes.
...The rest of the article omitted.

General idea first:
Suppose A, B, C, are fragments of shell expressions, and suppose that you
wish to enclose the expression  - A 'B' C - in a pair of single quotes.
[Note: I use hyphens to delineate the expressions; they are NOT parts of the
the expressions.]

The obvious solution - 'A 'B' C' - will not do, because in effect it creates
the quoted strings - 'A ' -  and  - ' C' -, and squeezes an unquoted - B - in
between.  This is important, so make sure you see this before reading on!

Now that you see this, the rest is easy:  What you want is - 'A '\''B'\'' C' -
which is parsed as  'A '  +   \'   + 'B'   +   \'  +   ' C'.   Get it?

Now back to your problem.  Applying the general idea above, you can see
that the statement:

alias killroy  \
  'kill '\`'ps ax | grep -w roy | grep -v grep | awk ' \''{print $1}'\'\`

does the trick.  In fact, the pair of quotes surrounding "kill" are not
necessary and may be dropped.

An alternative solution, somewhat uglier, is to drop all unnecessary single
quotes and to "escape" all special characters
in sight:

alias killroy  \
   kill \` ps ax \| grep -w roy \| grep -v grep \| awk \'\{print \$1\}\'\`

I hope that this helps.

Rouben Rostamian                               Phone: 301 455-2458
Department of Mathematics                      e-mail:
University of Maryland Baltimore Counnty       Rostamian at umbc.bitnet
Baltimore, MD 21228                            rostamia at umbc3.umbc.edu



More information about the Comp.unix.questions mailing list