#!/bin/sh # RecursiveGrep.sh # Chieh Cheng # 2000-9-14 # http://www.CynosureX.com/ # GNU General Public License (GPL), Version 2, June 1991 scriptName=`echo "$0" | sed s/.*\\\\///g` usage () { echo " Usage: ${scriptName} [ -d depth ] \"expression\" [ \"file mask 1\" ... \"file mask N\" ]" } if [ $# -lt 1 ] then usage else depthSet=0 depth=0 while getopts d: ch do case $ch in d) depthSet=1 depth=$OPTARG if [ $# -lt 3 ] then usage exit 2 fi ;; \?) usage exit 2 ;; esac done shift `expr $OPTIND - 1` executable=`AbsoluteName.sh "$0"` searchString="$1" if [ $# -eq 1 ] then fileNameSpec="*" else shift fileNameSpec="$@" fi currentPath=`pwd`/ dumpfile=`GetTempPathName.ksh "${scriptName}"` ls -1 ${fileNameSpec} 2> "${dumpfile}" | while read entry do if [ -f "${entry}" ] then result=`grep "$searchString" "${entry}"` if [ "${result}" != "" ] then pwd=`pwd` printf '%s/%s : \n' "${pwd}" "${entry}" echo "${result}" printf '\n' fi fi done rm "${dumpfile}" if [ ${depthSet} -eq 0 ] || [ ${depth} -gt 0 ] then if [ ${depthSet} -eq 1 ] then depth=`expr ${depth} - 1` flags="-d ${depth}" else flags="" fi # recurse into sub directory ls -1 | while read entry do if [ -d "${entry}" ] then if [ -x "${entry}" ] then cd "${entry}" "${executable}" ${flags} "${searchString}" "${fileNameSpec}" cd .. else echo "No permission to enter ${entry}." fi fi done fi fi