#!/bin/bash case "$0" in ( *egrep* ) grep="egrep" ;; ( *fgrep* ) grep="fgrep" ;; ( * ) grep="grep" ;; esac if [ "$#" = "0" -o "$1" = "-h" -o "$1" = "--help" ]; then cat <<-EOF usage: $0 [grep-args] [<.ext>] [ ...] Will use "grep" to search recursively through the given files or directories, which defaults to ".". ".svn" and ".git" directories are omitted. If an extension is given (an argument that begins with a "." and something other than a ".", can contain shell patterns), then only files of that extension will be grepped. If named as something with "egrep" or "fgrep", will use these for searching. EOF exit fi grep_opts=() pattern="" find_opts=() while getopts "e:m:nwxivqsolhHE" name; do case "$name" in ( e ) pattern="$OPTARG" ;; ( [nwxivqsolhHEc] ) grep_opts=( "${grep_opts[@]}" "-$name" ) ;; ( m ) grep_opts=( "${grep_opts[@]}" "-$name" "$OPTARG" ) ;; ( * ) echo "$0: internal error" 1>&2; exit 1 ;; esac done shift $(( OPTIND - 1 )) if [ "x$pattern" = "x" ]; then if [ "$#" = "0" ]; then echo "$0: missing pattern" 1>&2; exit 1 else pattern="$1"; shift fi fi case "$1" in ( .[^.]* ) if [ ! -e "$1" ]; then find_opts=( "${find_opts[@]}" "-name" "*$1" ); shift fi ;; esac if [ "$#" = "0" ]; then set "."; # Emacs automatically appends a /dev/null, so if this is the only one, use "." elif [ "$#" = "1" -a "$1" = "/dev/null" ]; then set "." fi # if not a single file or if a directory, add -H to show file names # (before other flags so it can be can ovverriden) if [ "$#" != "1" -o -d "$1" ]; then grep_opts=( "-H" "${grep_opts[@]}" ); fi find "$@" -name ".svn" -prune -o -name ".git" -prune -o -type f \ "${find_opts[@]}" -print0 \ | sort -z \ | xargs -0 "$grep" "${grep_opts[@]}" -e "$pattern"