RGrep - Grep récursif
Présentation
-
Ce script est une évolution de la commande Grep. Il permet d'effectuer la recherche dans le répertoire courant, et les sous-répertoires..
La syntaxe est la suivante:
-
rgrep <chaine_recherchee> <filtre>
-
rgrep jerome *
Le script
########################################################
## Script: rgrep (Recursive Grep) #
## Version: 1.00 - June 2008 #
## Author: Jerome DESMOULINS (http://www.desmoulins.fr) #
#########################################################
## This script is a recursive grep. Usage is the same #
## than grep Unix command. #
#########################################################
PN=`basename "$0"` # Program name
VER=`echo '$Revision: 1 $' | cut -d' ' -f2`
: ${GREP:=egrep}
FindOpts="-follow -type f"
GrepOpts=
#Command line usage
Usage () {
echo >&2 "$PN - recursive grep, $VER
usage: $PN [grep_opts | find_opts] SearchString [file | directory] ...
grep_opts: Options for $GREP (the default is \"$GrepOpts\")
find_opts: Options for find (the default is \"$FindOpts\")."
exit 1
}
#Show Message
Msg () {
for i
do echo "$PN: $i" >&2
done
}
#In case of fatal error
Fatal () { Msg "$@"; exit 1; }
Args=
while [ $# -gt 0 ]
do
case "$1" in
--) shift; break ;;
-h) Usage;;
-?) GrepOpts="${GrepOpts:+$GrepOpts }$1";;
-??*) Args="${Args:+$Args }$1";;
*) break ;; # First file name
esac
shift
done
[ -n "$Args" ] && FindOpts="$Args"
[ $# -lt 1 ] && Usage
Search="$1"; shift
[ $# -lt 1 ] && set -- . # Default search directory
exec find "$@" $FindOpts -print | xargs $GREP $GrepOpts "$Search" /dev/null