csh question -- aliasing, quoting and the like

Gertjan van Oosten gjoost at westc.UUCP
Tue Sep 19 22:42:49 AEST 1989


The problem concerns csh-aliases (aliasi ??? - David Addison in
'Moonlighting').
(There really should be a newsgroup for this -- comp.csh.questions??
 Let's start a vote!!)
The basic problem is as follows: I want an alias to kill all processes
called 'roy'.

Outline:

% 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.
Not being able to figure out how (and if) this 'quoting problem' can be
dealt with, the following solution was found (whitespace added for clarity):

% alias killroy 'set var=\$1 ; kill `ps ax | grep -w roy | grep -v grep |
                                     awk "{print $var}"` ; unset var'

This works, but has its drawbacks, one of them being that 'var' may be set
before executing the command; it will be unset afterwards. This can be
solved by introducing a subshell:

% alias killroy '(set var=\$1 ; kill `ps ax | grep -w roy | grep -v grep |
                                      awk "{print $var}"`)'

So far, so good.
Now comes the real problem: I want to kill all processes called 'roy' on
machine 'host'.

Outline ('rsh' is 'remote shell', not 'restricted shell' (who uses
restricted shells, anyway?)):

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

The easy way out is to add another alias:

% alias killhostroy 'rsh host killroy'

This is based on '~/.cshrc' being interpreted by 'rsh' before executing the
command; thus the 'killroy'-alias should be executed in '~/.cshrc'.
Another solution would be:

% alias killroy 'rsh host set var=\$1 ";" kill `ps ax | grep -w roy |
                                                grep -v grep |
                                                awk "{print $var}"`'

if `ps ax | ...` were executed on machine 'host'. However, this is not the
case: it is executed locally.

Aaargh. (Not Camargue...:-))

Does anyone out there have a more elegant (jus 'telegant - M.M.) solution?
Could somebody please enlighten me?
+----------------------------------------------------------------------+
| Gertjan van Oosten                                mcvax!westc!gjoost |
|                         West Consulting bv                           |
| Phoenixstraat 49          P.O. Box 3318           Tel: +31-15-123190 |
| 2611 AL  Delft            2601 DH  Delft          Fax: +31-15-147889 |
|                          The Netherlands                             |
+----------------------------------------------------------------------+
 No  Points   Name                                                     Hp [max]
  1    6141  HC-V was killed on level 6 by a hill orc.                  -  [66]



More information about the Comp.unix.questions mailing list