Article Listing 1 Listing 2 Listing 3 Listing 4
Listing 5 apr97.tar

Listing 5: Associative array usage

. shlib

GetProcs()
{
while read F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
do
if [ "$CMD" ]
then
arrset pid2cmd $PID $CMD
arrset pid2own $PID $UID
fi
done
}

User=`whoami`

KillProcs()
{
arrnames Procs pid2cmd
for Proc in $Procs
do
arrget Cmd pid2cmd $Proc
arrget Owner pid2own $Proc
if Matches "$Cmd" "$1"
then
if [ "$User" = "root" ]  || [ "$User" = "$Owner" ]
then
kill -9 $Proc
else
echo "Process <$Cmd> ($Proc) owned by $Owner, can't kill"
fi
fi
done
}

ps -edalf > /tmp/sc1.$$
GetProcs < /tmp/sc1.$$
for p
do
KillProcs "$p"
done


# End of File