mardi 28 avril 2015

Too many arguments error if statement (Debian - Bash)

Really need some help/guidance with this and would be much appreciated if someone could help me!

I'm basically trying to create a shell-script which if process name is entered it will kill it, however if it is a root process it won't.

Now the first part of the script works fine and I can kill process but can't get my head round how to do the if statement.

Please see code below

#!/bin/bash
echo -n 'Please enter process name:'
read process 
pid1=$( ps -aef | grep -v grep | grep $process | awk'{print $2}' )
pid2=$( ps -NU root | grep $process | awk '{print $1} ' )
if { $pid1 -eq $pid2 ];
echo Kill $process
killall -e $process
else
echo "Unable to kill $process as this is a root process."
fi

The error I'm getting when I enter a root process is line 6: [: too many arguments

Any help greatly appreciated

Aucun commentaire:

Enregistrer un commentaire