Jason Underdown’s Blog

Math, Physics and Free Software

Counting Lines of Code

with one comment

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

Written by Jason Underdown

June 5, 2007 at 2:44 am

Posted in Linux, Programming

One Response

Subscribe to comments with RSS.

  1. I found this software very useful for this task:

    http://cloc.sourceforge.net/

    Attila

    August 13, 2008 at 6:54 am


Leave a Reply