[comp.unix.questions] Problem piping "jobs" ** Summary **

schuetz@iraul1.ira.uka.de (Elmar Schuetz) (12/09/88)

In article <733@iraun1.ira.uka.de> I asked
> [whether it's possible to pipe the "jobs"-command or how to kill the
>  job with the highest number.]

Up to now I got answers from
	Mike Khaw <mkhaw@teknowledge-vaxc.ARPA>
	Ruben Gamboa <ruben@mcc.COM>
	Chris Torek <chris@mimsy.umd.EDU>
	Jonathan Sweedler <nsc!taux01!cjosta@uni-dortmund.DE>
	Michael Greim <greim@sbsvax.UUCP>
	Maarten Litmaath <maart@cs.vu.nl>
	Doug Gwyn <gwyn@brl.ARPA>

Everyone said that the shell built-in "jobs", when piped, forks a new subshell
which does NOT have job control enabled. Thus, it can't show any jobs of the
parents' shell. The work-around would be to redirect the output to a temp file.

Michael suggested to make changes to the csh in order to do a 'kill -9 %#'.
And Doug mentioned there would be a solution in the BRL Bourne shell.


Mike offered two aliases. One to be asked for the job to be killed:

	alias killbg 'jobs; echo -n "which one: "; kill -9 %$<'

and one to do what I wanted to do:

	alias klast 'jobs > jobs$$; \
		kill -9 %`tail -1 jobs$$ | \
		sed '"'"'s/\[\([0-9]*\)\].*/\1/'"'"'`; \
		rm -f jobs$$'

And a similar solution from Maarten:

	jobs -l > /tmp/jobs$$
	kill -9 `sed -n '$s/[^ ]*[^0-9]*\([0-9]*\).*/\1/p' /tmp/jobs$$`


My comment:
  Mike's solution does not work with 'alias', but that doesn't matter.
  Maarten's solution works fine and is faster without 'tail'.

So, many thanks to all who answered (i.e. Mike and Maarten for their effort)
and to all who's reply is on it's way.

Best regards, Elmar
--
There are many here among us who feel that life is but a joke.	-- Bob Dylan