Wicked Cool Shell Scripts
Wicked Cool Shell Scripts :: shell script 021-findman.sh

Shell Script 021-findman.sh

#!/bin/sh

# findman -- given a pattern and a man section, show all the matches
#   for that pattern from within all relevant man pages.

match1="/tmp/$0.1.$$"
matches="/tmp/$0.$$"
manpagelist=""

trap "rm -f $match1 $matches" EXIT

case $# 
in
  3 ) section="$1"  cmdpat="$2"  manpagepat="$3"	    ;;
  2 ) section=""    cmdpat="$1"  manpagepat="$2"	    ;;
  * ) echo "Usage: $0 [section] cmdpattern manpagepattern" >&2 
      exit 1
esac

if ! man -k "$cmdpat" | grep "($section" > $match1 ; then
  echo "No matches to pattern \"$cmdpat\". Try something broader?"; exit 1
fi

cut -d\( -f1 < $match1 > $matches	# command names only
cat /dev/null > $match1         	# clear the file...

for manpage in $(cat $matches)
do
  manpagelist="$manpagelist $manpage"
  man $manpage | col -b | grep -i $manpagepat | \
    sed "s/^/${manpage}: /" | tee -a $match1
done 

if [ ! -s $match1 ] ; then
cat << EOF
Command pattern "$cmdpat" had matches, but within those there were no 
matches to your man page pattern "$manpagepat" found in that set.
Man pages checked:$manpagelist
EOF
fi

exit 0

Explore The Book!
[book cover]
Table of Contents
Read Some Scripts!
Shell Script Library
Book Errata
All The Links
Read the Reviews
Talk About It
Author Bio
Buy The Book!



Other books by author Dave Taylor
Learning Unix for Mac OS X (O'Reilly & Associates)
Solaris 9 for Dummies (Wiley)
Teach Yourself Unix in 24 Hours (Sams/Macmillan)
Teach Yourself Unix System Administration in 24 Hours (Sams/Macmillan)
Creating Cool HTML 4 Web Pages (Wiley)
Ten Quick Steps to Learning Mac OS X Unix (ebook!)