Cannot kill hacker process with my script

I want to kill a process of xterm that is run by hacker with my login name.
So, I write a shell script to do my goal.
I run 2 xterm and then I run my script on a first xterm. it should kill the process of a second xterm but it doesn't.Why?

Here is my code :

#!/bin/ksh
myps=$(ps -f|grep xterm|cut -d ' ' -f3)
mytty=$(tty|cut -c 6-10)
hackerps=$(ps -fu $(logname)|grep xterm| cut -d ' ' -f3)
hackertty=$(ps -fu $(logname)| grep xterm | cut -d ' ' -f10)
set -A mycurrent $myps
set -A hackercurrent $hackerps
set -A hackerttycurrent $hackertty
i=0

ttyother=0
while [ $i -le 400 ]
#!/bin/ksh
myps=$(ps -f|grep xterm|cut -d ' ' -f3)
mytty=$(tty|cut -c 6-10)
hackerps=$(ps -fu $(logname)|grep xterm| cut -d ' ' -f3)
hackertty=$(ps -fu $(logname)| grep xterm | cut -d ' ' -f10)
set -A mycurrent $myps
set -A hackercurrent $hackerps
set -A hackerttycurrent $hackertty
i=0

ttyother=0
while [ $i -le 400 ]
do
 if [ $i -eq 390 ]
 then
     j=0
     for j in ${#hackerttycurrent
[*]}
     do
     if [ $hackerttycurrent[j] != $mytty ]
     then
        ttyother=$(( ttyother+1 ))
        kill -9 $hackercurrent[j]
        echo Kill $ttyother other terminal already
     else
     echo Now has $ttyother other terminal run by $(logname)
        echo None of other terminal was killed
     fi
     done
 fi
 echo $i

 i=$(( i+1))
done
banner Program Finish!

my process id is a string so it cannot kill by this command "kill -9 processid"?

I am not sure what the problem is but when using an array element in bash, you might want to write:

     kill -9 ${hackercurrent[j]}
#instead of
     kill -9 $hackercurrent[j]

That should be no problem - maybe you had an additional j at the end of your pid.

Best post your error message here or use set -x and echo to see what your variables contain in the various steps of your script.

Another thing that might trip you up is using cut. You are assuming that there are always two blanks between the user name and the process id. In the case where the PID is shorter than 5 digits then there will be more leading blanks, and the cut command will not pick up the right information. (I checked ps output from both a Linux and FreeBSD system and neither left justify the PID.) If I were writing the script, I'd do something like this:

ps -fu $USER | awk '{ print $2}'

which will generate a list of process IDs regardless of the number of blanks that separate the user name from the PID.

It's work but it kill my process too in sometime but sometime it does as my goal. Why?

#!/bin/ksh
myps=$(ps -f|grep xterm|awk '{print $2}')
mytty=$(tty|cut -c 6-10)
hackerps=$(ps -fu $(logname)|grep xterm| awk '{print $2}')
hackertty=$(ps -fu $(logname)| grep xterm | cut -d ' ' -f10)
set -A mycurrent $myps
set -A hackercurrent $hackerps
set -A hackerttycurrent $hackertty
i=0

ttyother=0
while [ $i -le 400 ]
do
 if [ $i -eq 390 ]
 then
     j=0
     while [ $j -lt ${#hackerttycurrent
[*]} ]
     do
     if [ ${hackerttycurrent[$j]} != $mytty ]
     then
        ttyother=$(( ttyother+1 ))
        kill ${hackercurrent[$j]}
        echo Kill $ttyother other terminal already
     else
     echo Now has $ttyother other terminal run by $(logname)
        echo None of other terminal was killed
     fi
     j=$(( j+1 ))
     done
 fi
 echo $i

 i=$(( i+1))
done
banner Program Finish!

You're going to have the same problem with the other cut command:

hackertty=$(ps -fu $(logname)| grep xterm | cut -d ' ' -f10)

For the times that it fails, there are probably enough extra blanks in the line that picking up 'field 10' isn't getting the tty, but some other string. Then when you compare these to your tty name they will never match and you'll kill your session. Use the same technique with awk to pick up the correct tty field from the ps output.

If you want to be doubly sure that you don't kill your xterm, you could pass the process id to the script (it's possible to have the script dig this info out, but easier to pass it in). If your script is called kill_hacker, the command line might look something like:

kill_hacker $PPID

and the code in the script could be changed to test that the process id you're about to kill isn't the one passed in:

dont_kill=$1           # get xterm pid that we want not to kill
if [[ -z $dont_kill ]]
then
      echo "abort: missing process-id on command line"
      exit 1
fi
:
:
while (( $j < ${#hackerttycurrent[*]} ))
do   
        if [[ ${hackercurrent[$j]} != $dont_kill ]]
        then
                kill -9 ${hackercurrent[$j]}
                echo " killed: pid=${hackercurrent[$j]} tty=${hackerttycurrent[$j]}"
        else
                echo "skipped: pid=${hackercurrent[$j]} tty=${hackerttycurrent[$j]}"
        fi

        j=$(( j+1 ))
done

With this you could also elminate the need to get the tty information from the ps output.

Thank you so much!!!

Hi thsecmaniac ...

can you give me the full script ... i little bit confused if i see the full script i could understand better . Because i am in need to similiar thing.

I really posted a full script.