From: Iain Patterson Date: Wed, 3 Oct 2007 07:15:01 +0000 (+0000) Subject: phier now uses getopts. X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=7678bd5e61aed2294a984ffcdc3dc44baaa0c641;p=profile.git phier now uses getopts. Old usage: phier 1234 sshd. New usage: phier -s sshd 1234. git-svn-id: https://svn.cambridge.iain.cx/profile/trunk@75 6be0d1a5-5cfe-0310-89b6-964be062b18b --- diff --git a/phier b/phier index f2aad9f..eaa1b14 100755 --- a/phier +++ b/phier @@ -1,9 +1,23 @@ #!/bin/bash +# +# $Id$ +# +# phier: Show process tree incorporating a process. +# Notes: phier functions similarly (but not identically) to Solaris ptree. +# Usage: phier [options] +# Options: -s Stop hierarchy at this process name. +# Example: phier -s sshd $$ +# function find_parent() { child="$1"; shift top="$1"; shift + if [ -z "$child" ]; then + echo >&2 "No child to find." + return 1 + fi + cmd=$(ps -o comm= -p $child) pid=$(ps -o pid= -p $child) ppid=$(ps -o ppid= -p $child) @@ -22,7 +36,7 @@ function find_parent() { fi fi - if [ $ppid = 1 ]; then + if [ $pid = 1 -o $ppid = 1 ]; then echo $pid return 0 fi @@ -31,16 +45,24 @@ function find_parent() { return $? } +# Parse arguments. +top= +while getopts ":s:" opt; do + case $opt in + s) top="$1";; + esac +done +shift $((OPTIND-1)) + if [ $# -lt 1 ]; then echo >&2 "phier: Print process hierarchy for a process." - echo >&2 "Usage: phier []" - echo >&2 "Notes: If a search term is given the hierarchy will stop after finding a" - echo >&2 " process matching that search term." - echo >&2 "Example: phier $$ sshd" + echo >&2 "Usage: phier [options] " + echo >&2 "Options: -s Stop hierarchy at this process name." + echo >&2 "Example: phier -s sshd $$" exit 1 fi -parent=$(find_parent ${1+"$@"}) +parent=$(find_parent "$1" "$top") if [ $? = 0 ]; then pstree -ulap $parent else