Counting Lines of Code
I recently found myself frequently using the wc -l command line utility to count lines of code in my current project. But this got old quickly so I wrote a simple Bash script to do everything for me. Here it is:
#!/bin/bash
# This script determines the number of lines of code in
# all source and header files excluding the GPL banner
# at the beginning of each file. It doesn't count code
# in C files generated via gengetopt.
HEADERS=`ls -l ./include/*.h | wc -l`
CODE=`ls -l ./src/*.cc | wc -l`
# The GPL banner is 17 lines long
GPL_LINES=$[17*($HEADERS + $CODE)]
COUNT=0
for i in $( ls ./include/*.h ./src/*.ggo ./src/*.cc ); do
OUTPUT=`wc -l $i`
NUM=`expr match "$OUTPUT" '\([0-9]*\)'`
COUNT=$[$COUNT + $NUM]
done
COUNT=$[COUNT - $GPL_LINES]
echo "Total lines of code (minus GPL lines) = $COUNT"
This is my current output for my Kaliope project (gravitational N-body simulator/integrator).
$ ./code_diagnostics.sh Total lines of code (minus GPL lines) = 2322

I found this software very useful for this task:
http://cloc.sourceforge.net/
Attila
August 13, 2008 at 6:54 am