: ########################################################################## # Title : pidof - get process id (PID) from name # Author : Heiner Steven # Date : 1996-09-11 # Requires : # Category : System Utilities # SCCS-Id. : @(#) pidof 1.6 11/07/26 ########################################################################## # Description # ########################################################################## PN=`basename "$0"` # Program name VER='1.6' Usage () { echo >&2 "$PN - get process id (PID) from name, $VER usage: $PN [-av] name [...] -a: search process arguments for given name, too -v: print verbose output $PN only matches complete names." exit 1 } relaxed=false # Check arguments, too (true/false) verbose=false while getopts :av Opt do case "$Opt" in a) relaxed=true;; v) verbose=true;; ?) Usage;; esac done shift `expr $OPTIND - 1` [ $# -gt 0 ] || Usage # System V style "ps" cuts the command name to eight characters for "ps -c" shortname=false # Check which options to use with this system if ps -ef > /dev/null 2>&1 then # System V style if [ $relaxed = true ] then PS="ps -ef"; Col=2 else PS="ps -e"; Col=1; shortname=true; fi else # BSD style if [ $relaxed = true ] then PS="ps -aux"; Col=2 else PS="ps -cax"; Col=1 fi fi # Build one "awk" search pattern from all command names: # "(cmd1|cmd2|cmd3)" Pattern= for Name do # Quote each character by including it in square brackets, e.g. # "test+.x" -> "[t][e][s][t][+][.][x]" # # A quoted expression using backspaces would be shorter, but is # harder to get right in a portable way. cmd=`echo "$Name" | sed 's/./[&]/g'` if [ $shortname = true ] then # Some systems use a short name, some do not use it. We search # for both the short and the long name short=`echo "$Name" | cut -c1-8 | sed 's/./[&]/g'` fi Pattern="${Pattern:+$Pattern|}$cmd|$short" done Pattern="[ \/]($Pattern)([ ]|$)" if [ $verbose = false ] then $PS | awk "/$Pattern/ { print \$$Col }" else $PS | awk "/$Pattern/ { print }" fi