Using awk to kill a process

Hi Guys
I am running a small script to find a process id , ask user if they want to kill the process and then action it Yes or no

Here is what i have

#/bin/bash


ps -l | grep -i $1 | awk '{print "Process id of '$1' is "$4""}' 

echo "Do you want to kill this process (Y/N)"
read action

if [ $action = "Y" ]
	then
	kill  $4
		elif [ $action = "N" ]
		then
		echo "Ok, thanks for using the program"
		exit 0
fi 

Having small problem with the $4 as outisde of the awk command, the $4 means nothing. Is there any way to can take $4 into the if else command to kill it ?

usage for example is ./script gedit
It gives me back the process id of gedit ok but its after that i need a little help

Why do you use awk for that?

using awk as i just want to get familiar with the command

Try

read PID CMD <<< $(ps -eopid,command|grep [g]edit)

I want to use the awk command like in my above script

Hello

Allthough awk's first output column ($1) matches what you have passed $1, it is NOT the same.
One is a shell variable, and one is a column ID.

You can pass a variable to AWK, with awk -v VAR=value ... , however, this is only to pass a shell variable into awk.
You might catch the awk output into a shell variable, and compare that for further shell actions.

You do need to catch the proccess id (pid) of the passed argument ($1) into a new variable, and 'kill that'..

value=$(ps -l | grep -i $1 | awk '{print $4}')

hth

1 Like

You do need to catch the proccess id (pid) of the passed argument ($1) into a new variable, and 'kill that'..

How can i catch the process id of the passed arguement ? I just need to awk for the print command to print the pid of the arguement

---------- Post updated at 07:39 AM ---------- Previous update was at 07:15 AM ----------

thanks
got this working with

#/bin/bash

ps -l | grep -i $1 | awk '{print "Process id of '$1' is "$4""}' > output.txt

output="output.txt"


pid=$(cat "$output" | cut -d ' ' -f6)


echo "Do you want to kill this process (Y/N)"
read action

if [ $action = "Y" ]
	then
	kill  $pid
		elif [ $action = "N" ]
		then
		echo "Ok, thanks for using the program"
		exit 0
fi 

There is no need to save the output as a file.
Have a look at:

PID=$(ps -l | grep -i "$1" | grep -v grep | awk '{print $4}')
if [ -n "$PID" ]
then	echo "Do you want to kill $1 ($PID)?"
	read -n1 buffer
	case $buffer in
	y|Y)	kill $PID	;;
	n|N)	echo "Ok, thanks for using the program"	;;
	esac
fi

Hope this helps

1 Like

can you explain the following to me ?

grep -v grep
if [ -n
read -n1 buffer
case $buffer in

The -n checks if the following variable, in quotes, is Not-emtpy.

Since you grep a variable, you not only get the line you are looking for, but also the grep line itslf.
grep -v grep removes that unwanted line.

If you check for something 'hardcoded' this could be simplified as:

grep [v]ersion file.txt

This would print only a line with the word version, but not the grep cmd looking for it.

The read -n1 buffer , will read 1 char the user inputs, and then continue with the code.

Hth

1 Like

excelent thanks . i really like the read -n1 buffer