Skip to content

Instantly share code, notes, and snippets.

@kamend
Forked from danomatika/check-for-src-pattern
Created February 23, 2014 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamend/9173683 to your computer and use it in GitHub Desktop.
Save kamend/9173683 to your computer and use it in GitHub Desktop.
#! /bin/bash
#
# simple script to check for a given string pattern in any
# .c, .h, .cpp, .cxx, .m, & .mm source files
#
# Dan Wilcox <danomatika@gmail.com> 2014
#
# super simple command parsing
if [ $# -lt 1 -o "$1" == "-h" -o "$1" == "--help" ] ; then
echo "Usage: check-for-src-pattern \"pattern to match\""
exit 0
fi
PATTERN="$1"
echo "Checking for \"$PATTERN\" ..."
echo ""
# set internal field separator to endline so filenames with spaces are not broken up
IFS=$'\n'
# get a list of source files in the given dir
files=$(find . -type file \( -name *.c -o -name *.h -o -name *.cpp -o -name *.cxx -name *.m -o -name *.mm \))
for file in ${files[*]} ; do
# check against the given pattern, print the filename and grep output if we find a match
found=$(grep -n "$PATTERN" "$file")
if [ "$found" != "" ] ; then
echo "$file:"
echo $found
echo ""
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment