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

Advertisements

One thought on “Counting Lines of Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s