Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#! /bin/bash
# simple script to check for a given string pattern in any
# .c, .h, .cpp, .cxx, .m, & .mm source files
# Dan Wilcox <> 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
echo "Checking for \"$PATTERN\" ..."
echo ""
# set internal field separator to endline so filenames with spaces are not broken up
# 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 ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment