The script below is a simple lines of code counter for you. Following interesting features of the script are:
- It recursively counts the lines of code within all subdirectories of the location mentioned.
- It counts the lines of code of only those files whose extensions are considered as source file name extensions in the list srcFileExtensions defined early in the script.
- Optionally, you could make it exclude the blank lines from the lines of code.
Caution: The script doesn't run like lightning! Suggested optimisations will be gratefully accepted.
Also, there are plenty of superior LOC tools out there. Just google! But for simple plain lines of code counting, this one would still be good for you to begin with. Just copy paste into a bash shell script and go!
Also, there are plenty of superior LOC tools out there. Just google! But for simple plain lines of code counting, this one would still be good for you to begin with. Just copy paste into a bash shell script and go!
#!/bin/bash
FALSE=0;
TRUE=1;
srcFileExtensions=( \
# java # Java
# cpp # C++
# cc # C++
# h # C, C++
# hh # C++
# hpp # C++
# c # C
# py # Python
# pl # Perl
sh # Shell
# flex # flex
# lex # lex
# y # yacc
# yy # yacc
# cs # C#
); # File name extensions which will be considered as source files. Remove/add as needed. Keep the list to minimum to keep the speed good.
function loc_dir(){
local prefix=$1;
if [ ! -d ${prefix} ]; then
echo "${prefix} is not a directory. Quitting...";
exit 1;
fi
local names=( `ls ${prefix}` );
for name in ${names[@]}; do
local fullName=${prefix}/${name};
if [ -d "${fullName}" ]; then
loc_dir "${fullName}"
elif [ -f "${fullName}" ]; then
isSrcFile ${fullName};
local result=$?;
if [ "${result}" == "${TRUE}" ]; then
echo "including ${fullName} ...";
cat "${fullName}" >> "${locFile}";
fi
else
echo "Something wrong with ${fullName}";
fi
done;
}
function remove_empty_lines(){
local prefix=$1;
echo "Removing empty lines...";
local loc="${prefix}/loc.dat";
local loc1="${prefix}/loc1.dat";
mv ${loc} ${loc1};
while read line
do
if [ "${line}" != "" ]; then
echo ${line} >> ${loc};
fi
done <${loc1}
rm ${loc1};
}
function isSrcFile(){
local fileName=$1;
for ext in ${srcFileExtensions[@]}; do
local extLength=`expr length ".${ext}"`;
local nameLength=`expr length "${fileName}"`;
local startPosition=`expr ${nameLength} - ${extLength}`;
local len=`expr ${nameLength} - 1`;
local subString=`echo ${fileName:${startPosition}:${len}}`;
if [ "${subString}" == ".${ext}" ]; then
return ${TRUE};
fi
done;
return ${FALSE};
}
# main
dirname="";
if [ $# == 0 ]; then
dirname="\.";
else
dirname=$1;
fi
locFile="${dirname}/loc.dat";
loc_dir ${dirname};
# remove_empty_lines ${dirname};
if [ -f "${locFile}" ]; then
result=( `wc -l "${locFile}"` );
else
result=0;
fi
loc=${result[0]};
echo "${loc} lines of code."
rm "${locFile}";
No comments:
Post a Comment