: ########################################################################## # Title : pidof - get process id (PID) from name # Author : Heiner Steven # Date : 1996-09-11 # Requires : # Category : System Utilities # SCCS-Id. : @(#) pidof 1.5 05/08/24 ########################################################################## # Bugs # o Some special characters ("*", ...) break the awk expression ########################################################################## PN=`basename "$0"` # Program name VER='1.5' 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 cmd=$Name 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 cmd="$cmd|`echo \"$Name\" | cut -c1-8`" fi Pattern="${Pattern:+$Pattern|}$cmd" done Pattern="[ \/]($Pattern)([ ]|$)" if [ $verbose = false ] then $PS | awk "/$Pattern/ { print \$$Col }" else $PS | awk "/$Pattern/ { print }" fi