代码行数统计

https://github.com/lixuande/countcodeline
有些小项目做完,做项目总结或者验收的时候,会有一个代码量的统计,之前多是给一个估计量,其实用脚本统计也是挺方便的,只需要挨个遍历路径下的文件,统计每个文件的代码行数就可以了
下面是一个统计路径下代码行数的小脚本countcodeline.sh:

#!/bin/bash
g_count=0
function countcodeline()
{
    filename=$1
    fileformart=`echo ${filename##*.}`
    if [ $fileformart = "h" ] || [ $fileformart = "hpp" ] \
    || [ $fileformart = "c" ] || [ $fileformart = "cpp" ];
    then
            line=`cat $filename|wc -l`
            echo "$filename--$line--"
            let g_count+=line
    fi
}

function findcodefile()
{
    for element in `ls $1`
    do
            dir_or_file=$1"/"$element
            if [ -d $dir_or_file ];
            then
                    findcodefile $dir_or_file
            else
                    countcodeline $dir_or_file
            fi
    done
}

codefiledir=$1
findcodefile $codefiledir
echo $g_count

使用方法也比较简单:sh countcodeline.sh /home/test/buildermodule/src
这样就可以统计/home/test/buildermodule/src下的所有h、hpp、c、cpp代码的行数。

猜你喜欢

转载自blog.csdn.net/lixuande19871015/article/details/80843446