#!/bin/zsh 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 default to ".". ".svn" 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:nwxivqsolhH" name; do case "$name" in (e) pattern="$OPTARG" ;; ([nwxivqsolhH]) grep_opts=( $grep_opts "-$name" ) ;; (m) grep_opts=( $grep_opts "-$name" "$OPTARG" ) ;; (*) return 1 ;; esac done shift $(( OPTIND - 1 )) case "$1" in (.[^.]*) find_opts=( $find_opts -name "*$1" ) ; shift ;; esac if [ "x$pattern" = "x" ]; then if [ "$#" = "0" ]; then echo "sgrep: missing pattern" 1>&2; return 1 else pattern="$1"; shift fi fi if [ "$#" = "0" ]; then 1="."; # Emacs automatically appends a /dev/null, so if this is the only one, use "." elif [ "$#" = "1" -a "$1" = "/dev/null" ]; then 1="." 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 "$#" ]; then grep_opts=( -H $grep_opts ) fi echo $0 find "$@" -name ".svn" -prune -o $find_opts -print0 \ | xargs -0 "$grep" $grep_opts -e "$pattern"