#!/bin/sh
#
# killpid -- Kill or list running processes by name
#
# Copyright 2007-2016.
#
# $Compile: ./killpid$
# $Maintained at: http://www.visualco.de$
# $Author: Andreas Spindler <info@andreasspindler.de>$
# $Writestamp: 2016-04-20 12:55:56$

Usage() {
    cat <<EOF 1>&2
USAGE
    `basename $0` {-k|--kill} [process-name]...
EOF
}

optkill= optdry=1
while [ $# -gt 0 ]; do
    case "$1" in
        -h|--help) Usage;;
        -k|--kill) optkill=1; optdry=;;
        *) names="$names $1"
    esac
    shift
done

if [ -z "$names" ]; then
    ps -u $UID
else
    if [ -n "$optkill" ]; then  # send SIGKILL to all
        kill -9 `$0 $names`
    else                        # simply output the list of PIDs
        for p in $names
        do ps -a $p | awk "/$p/ { print \$1 }"
        done
    fi
fi

# Local Variables:
# coding: iso-8859-1-unix
# fill-column: 80
# End: