[comp.unix.questions] Funny kill -9 behaviour

jharkins@sagpd1.UUCP (Jim Harkins) (08/18/90)

Due to inattention on my part I managed to create a bunch of processes I
needed to kill.  Rather than typing 'kill -9 number-list', I tried:

	kill -9 `ps -aug | grep ekg | awk '{print $2}'`

To which I was rewarded with

	`ps -aug | grep ekg | awk '{print $2}'`: ambiguous.

After playing around a bit I realized the problem lay with the kill -9,
by replacing it with echo I got a list of process numbers.  I redirected
the ps ... awk output into foo, then tried

	kill -9 `cat foo`

and got

	`cat foo`: ambiguous.

What I want to know is why in the hay does this happen?  FYI, I killed the
turkeys with a foreach i (`cat foo`) loop in which I kill -9'd the $i's.
Also, I'm on a Sun running 4.3 bsd.


-- 
jim		jharkins@sagpd1

Iraqnophobia!  The talk of the Middle East.  Coming soon to a theatre of war
near you.

guy@auspex.auspex.com (Guy Harris) (08/19/90)

>What I want to know is why in the hay does this happen?

Because the C shell is flakier than a snowstorm.  To quote the BUGS
section of the SunOS 4.0.3 manual page (this quote is also in the S5R4
"csh" manual page):

     Although robust enough for general use, adventures into  the
     esoteric  periphery  of  the  C  shell may reveal unexpected
     quirks.

which translates as "the C shell is flakier than a snowstorm."

Try doing "/usr/bin/kill `cat /tmp/foo`" instead.  "kill" is a C shell
builtin; somehow, for *some* but not *all* builtins, that construct
seems to confuse the shell.  Using the non-built-in "kill" seemed to
make the problem go away, at least when I tried it.

chris@mimsy.umd.edu (Chris Torek) (08/19/90)

In various articles various people ask why

	kill -9 `some expression`

produces

	`some expression`: Ambiguous

The reason is that kill is a C-shell built-in, these people are using
the C-shell, and the C-shell is (to an amazingly large extent, given
that it `works' for most uses) quite thoroughly broken inside.  The
kill code expands the backquote expression, finds that it breaks into
several words, and complains (instead of iterating over each of the
words).  This does not happen with

	kill -9 several words

because here the expansion occurs before the built-in kill function.

Note that kill *must* be a built-in, since `kill %2' has to convert the
`%2' to a `job' (and %n does not normally expand to a list of process
IDs, which is the obvious way to do this without a built-in).

So: workarounds:

	eval kill arguments `expression`

	/bin/kill arguments `expression`

The latter runs one more process, does not interpret `%'s, and is
dependent on the location of the `kill' program.  The former simply
causes the backquote expression to be expanded before calling the
internal `kill' routine.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris
	(New campus phone system, active sometime soon: +1 301 405 2750)

maart@cs.vu.nl (Maarten Litmaath) (08/20/90)

In article <26072@mimsy.umd.edu>,
	chris@mimsy.umd.edu (Chris Torek) writes:
)...
)	/bin/kill arguments `expression`
)
)The latter runs one more process, does not interpret `%'s, and is
)dependent on the location of the `kill' program.  [...]
 ^^^^^^^^^^^^^^^^^^^^^^^^^
Try this:
		\kill ...
--
        "[Your C code] seems about as portable as the Rock of Gibraltar."
                                 (Wayne Throop)

volpe@underdog.crd.ge.com (Christopher R Volpe) (08/22/90)

I'd like to know why this is considered "ambiguous". What exactly
are the two possible interpretations of that line of text that
the C shell can't decide between?

==================
Chris Volpe
G.E. Corporate R&D
volpecr@crd.ge.com