Q:

Redirecting the output of ps command,getting process id and killing that process using shell script

I want to write a shell script to find the running process for a given user and kill the process by getting the respective process ID.
Its like
ps -ef | grep dinesh

After this, i am getting the output as the following
dinesh 19985 19890  0 11:35 pts/552  00:00:00 grep dinesh

Here 19985 is the process ID. I want to kill that process.
How can i achieve this using script?
I have to parse the ps command output and get the process ID
Thanks in advance.

A:

kill `ps -ef | grep dinesh | awk '{ print $2 }'`

A:

What if there is more than one process defined by the string 'dinesh'?  What about the grep process itself?  This is a more complete answer
ps -ef | grep dinesh | grep -v grep | awk '{print $2}' | xargs kill -9

