Skip to content

Instantly share code, notes, and snippets.

@kckrinke
Last active November 9, 2016 22:27
Show Gist options
  • Save kckrinke/f86a2b172c4cd661a9b3c0d4219f4bab to your computer and use it in GitHub Desktop.
Save kckrinke/f86a2b172c4cd661a9b3c0d4219f4bab to your computer and use it in GitHub Desktop.
Like the find-src.sh script but greps the files found via find-src.sh mechanics.
#!/bin/bash
usage() {
while [ $# -gt 0 ]; do echo "$1"; shift; done
echo "usage: $(basename $0) [options] <PATTERN> [path ...]"
echo "options:"
echo " -? This usage information screen"
echo " -N Dry run, just print command"
echo " -t Explicit source file extn"
echo " -i Case-insensitive (egrep)"
echo " -L Filenames without match (egrep)"
echo " -l Filenames with match (egrep)"
echo " -h No filename (egrep)"
echo " -n Line number (egrep)"
echo " -Z Output NULL instead of newline (egrep)"
echo
echo "Note: defaults to \"grep-src -t sh -t pl -t php -t py\""
exit 1
}
GREP_OPTS=()
DRY_RUN=0
CONDS=()
while getopts ":?Nt:viLlhnZ" ARG "$@"
do
case "${ARG}" in
"?") usage ;;
"N") DRY_RUN=1 ;;
"t")
[ ${#CONDS[@]} -gt 0 ] && CONDS+=("-o")
CONDS+=("-name" "\"*.${OPTARG}\"")
;;
"i") GREP_OPTS+=("-i") ;;
"L") GREP_OPTS+=("-L") ;;
"l") GREP_OPTS+=("-l") ;;
"h") GREP_OPTS+=("-h") ;;
"n") GREP_OPTS+=("-n") ;;
"Z") GREP_OPTS+=("-Z") ;;
"--")
break
;;
esac
done
shift $((OPTIND-1))
[ $# -eq 0 ] && usage "Error: PATTERN is a required argument."
PATTERN="$1"
shift
if [ ${#CONDS[@]} -eq 0 ]
then
CONDS+=("-name" "\"*.sh\"")
CONDS+=("-o")
CONDS+=("-name" "\"*.pl\"")
CONDS+=("-o")
CONDS+=("-name" "\"*.php\"")
CONDS+=("-o")
CONDS+=("-name" "\"*.py\"")
fi
COMMAND=()
COMMAND+=("egrep")
for g in "${GREP_OPTS[@]}"
do
COMMAND+=("${g}")
done
COMMAND+=("${PATTERN}")
COMMAND+=('`')
COMMAND+=("find")
COMMAND+=("$@")
for c in "${CONDS[@]}"
do
COMMAND+=("${c}")
done
COMMAND+=('`')
if [ $DRY_RUN -eq 1 ]
then
echo "${COMMAND[@]}"
exit 0
fi
eval ${COMMAND[@]}
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment