[Code Snipplet] Counting the lines in a Cocoa project

Tipo wanted to generate some stats (aaaaaaaaaaah yes, we developers just love stats) for his project. One of the good things about Mac OS X, is that we have access to all the wonderful UNIX underpinnings… Shell script to the rescue!


find . -name "*.[mh]" -type f | while read filename
incr=`wc -l "$filename" | awk '{print $1}'`
countlines=$(($countlines + $incr))
echo $countlines > /tmp/.tmpCountLines

countlines=`cat /tmp/.tmpCountLines`
rm /tmp/.tmpCountLines

echo "$PWD : $countlines lines of code in .h and .m files"

Quick note : Apparently, the while works fine, but if you pipe something into it, variables aren’t saved… So I chose to write in a temporary file to keep the value across the script… Not perfect, but if anyone has an alternative, I’ll take it.


Leave a Reply