Source program from http://www.unixreview.com/documents/urm0108e/
The statement in the original program that filters out its own process has a bug. If you put killprog in the path of a $ PATH variable and then start killprog from other paths, $0 carries a path, later, I changed $0 to ${0 # */} to filter it out.
Later, I wrote an email to the author. I want to discuss whether others have ignored me. Is it because his English skills are too poor?
Run the following command in the Korn shell:
#! /Bin/KSh
# Killprog: Kill A program by name instead of by process ID.
# If there is more than one copy, display a list, and
# Ask to pick the right one.
# Author: Fred Brunet
# Modify: gehongbin 2001.8.30 $0 =>$ {0 ##*/}
Kill =/tmp/killprogout. $
If [$ #-EQ 0]
Then
Printf "Usage: killprog name \ n"
Exit 1
Fi
# Eliminate present program and 'grep $ 1' from process
PS-Ef | grep-V "grep $1" | grep-V ${0 ##*/} | grep $1> $ kill
Count = 'wc-L $ kill | awk '{print $1 }''
If [$ count-ge 1]
Then
Awk '{printf "% 2D: % s \ n", NR, $0}' <$ kill
Else
Printf "no matches to kill \ n"
If [-F $ kill]
Then
Rm $ kill
Fi
Exit 1
Fi
Printf "which of the above do you mean: Enter number or '0' to quit :"
While true
Do
Read lineno
# Non-numeric data returns 0
Lcnt = 'echo $ lineno | awk '{if ($0 ~ /[^ 0-9]/)
Print 0
Else
Print 1 }''
If [$ lcnt-EQ 0]
Then
Printf "valid input: 1-$ count or '0' to quit. reenter number :"
Continue
Fi
# Default to 0 and quit
Lineno =$ {lineno: = 0}
If [$ lineno-EQ 0]
Then
If [-F $ kill]
Then
Rm $ kill
Fi
Exit 1
Fi
If [$ lineno-GT $ count-o $ lineno-lt 0]
Then
Printf "valid input: 1-$ count or '0' to quit. reenter number :"
Else
Break
Fi
Done
Flist = 'sed-n "$ {lineno} p" $ kill'
Echo $ flist | awk '{printf "% s \ n", $0 }'
Killproc = 'echo $ flist | awk '{print $2 }''
Printf "Kill ($ killproc )? Y/N"
Read answer
Case $ answer in
Y | Y) Kill $ killproc
Printf "$ killproc terminated! \ N ";;
*)
Printf "Nothing killed \ n ";;
Esac
If [-F $ kill]
Then
Rm $ kill
Fi
# End killprog