BASH Pipe question

Hi all,

I'm new to shell scripting, but enjoying myself! :cool:

I'm trying to execute the following statement:
alias evol="ps -AH | grep evol | cut -d' ' -f2 | kill -9"

As you might guess. I'm wanting to use a single command 'evol' to kill all the processes containing the phrase 'evol' (having some trouble with my evolution-exchange connector which keeps hanging on address look-ups. Anyway - killing evolution isn't enough - I thenm have to kill all the other processes, usually 4 of them. the repetition of searching for the pids and then killing them is getting to me :mad:).

I find the bit up until the last pipe is fine. It produces a nice little list of pids to kill. After that though, kill throws an error. I suspect its something to do with the format of the list I'm trying to pipe to it...:confused:

Any help?

Regards, Mark

hi,
cut -d is unreliable in this case, as the ps output is formatted. so if you have two process ids of different length (i.e., 345, 23456) there will be leading spaces on the shorter one.

instead, use 'cut -c1-5'

then, you have to embed your command in a loop, like this:

for each in `command`;do kill -9 $each;done

the var $each is automatically created. the result of command in backticks is a list over which the for loop iterates.

hth,
dv

Thanks very much - that's exactly what I wanted to do.
I have subsequently worked out I can achieve the same result with 'killall -9 -ir evol*' which is briefer. However, your answer addresses the underlying question I had of how to get a the listed (piped) results to be loped through.

Thanks very much!!!:rolleyes:

killall behaves differently between different operating systems, so beware. Check if pkill is available on your OS, as that's likely to be safer. :slight_smile:

alias evol="kill -9 $(ps -AH | grep evol | awk '{ print $2 }')"