來源程式來自 http://www.unixreview.com/documents/urm0108e/
原來程式中過濾掉自身進程的那個語句有bug,如果把killprog放到一個$PATH變數的路徑中,然後從其他路徑啟動killprog時,$0是帶有路徑的,後來我把$0改為${0##*/},就可以過濾掉了
後來我還給這個作者寫了個email,想探討一下,別人也沒有理我,難道是他的英語水平太差,呵呵
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