: ########################################################################## # Shellscript: pidof - get process id (PID) from name # Version : 0.1 (beta) # Author : Heiner Steven (heiner.steven@odn.de) # Date : 11.09.1996 # SCCS-Id. : $Id: pidof,v 1.1.1.1 1999/06/15 19:29:05 heiner Exp $ ########################################################################## # Bugs # o SysV "ps -c" output truncates the command to 7 characters # o Some special characters ("*", ...) break the awk expression ########################################################################## PN=`basename "$0"` # Program name VER='0.1 (beta)' Usage () { echo >&2 "$PN - get process id (PID) from name, $VER (hs '96) 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 # 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 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 Pattern="${Pattern:+$Pattern|}$Name" done Pattern="[ /]($Pattern)([ ]|$)" if [ $verbose = false ] then $PS | awk "/$Pattern/ { print \$$Col }" else $PS | awk "/$Pattern/ { print }" fi