#!/bin/sh KILL=false ALL=false while : ; do case "$1" in "-"*) case "$1" in "-k") KILL=true ;; "-a") ALL=true ;; "--") shift 1 break ;; esac shift 1 continue ;; esac break done if [ -z "${1:+s}" ];then echo >&2 "usage: pq [OPTS] PATTERN" echo >&2 " PATTERN to grep" echo >&2 " -a all processes" echo >&2 " -k to grep and kill" echo >&2 " -- OPTS end" exit 1;fi A=$(ps opid,command x$($ALL&&echo "a") --no-header |grep "$1" |awk '{ print $1; }') A=$(echo "$A"|sed '/^'$$'$/d') I=$(echo "$A"|awk '{ print "ps opid >/dev/null "$0"||printf \"%s\" "NR"\"d;\"" }'|sh) A=$(echo "$A"|sed "$I") e=$(echo "$A"|awk 'BEGIN{ i=0 } NR==2{ i=1 } END{ print i }') { ${A:+ps u $A}; }>&2 if [ $e != 0 ];then echo >&2 "pq: 2 or more found" exit 1;fi if [ -z "${A:+s}" ];then echo >&2 "pq: 0 found" exit 1;fi if $KILL; then kill $A; fi