Io farei cosi`

codice:
  1 #!/bin/bash
  2 
  3 utente=""; processo=""; active=0;
  4 while [ $# -gt 0 ];
  5 do
  6     case "$1" in
  7         -a)
  8             active=$2
  9             shift;
 10         ;;
 11         -u)
 12             utente=$2
 13             shift;
 14         ;;
 15         -p)
 16             processo=$2
 17             shift;
 18         ;;
 19         -h|-*)
 20             echo "usage: $0 -u [utente] -p [processo] -a [attivi]"
 21             exit 1;
 22         ;;
 23     esac
 24     shift;
 25 done
 26 
 27 line=`ps -u $utente -U $utente u | grep $processo | grep -v "grep" | grep -v $0 | awk '{print $2}' | sort -n | wc -l`
 28 tokill=`echo "${line}-${active}" | bc`
 29 kill -9 `ps -u $utente -U $utente u | grep $processo | grep -v "grep" | grep -v $0 | awk '{print $2}' | sort -n | head -n $tokill`
ti apre 7 xclock
for i in `seq 1 7`; do xclock &; sleep 5; done

uccide i primi 5 e ne lascia attivi 2
./killolder -u $USER -p xclock -a 2