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
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'..
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