首页 > 其他 > 详细

代码行统计脚本

时间:2014-03-18 13:23:52      阅读:566      评论:0      收藏:0      [点我收藏+]

功能

最近无聊,写了一些代码,当我想统计这些代码的时候,却无法从网上找到合适的统计工具,所以便自己写了一个脚本,量身定制,用着比较舒服。

写在自己的博客上,供有需要的朋友们取用。

这是一个shell脚本,实现的功能如下:

1、统计注释行数(C和C++注释格式,其他语言的注释,在此脚本中没有考虑)

2、统计除去注释之外的空行数

3、统计除去注释和空行之外的代码行数

4、统计文件总行数

在代码中,对所统计的文件类型做了限制,默认只统计".c .h"后缀文件。自己可以根据需要,酌情修改。

该脚本可以同时输入多个目录作为参数,输出可以自己选择:每个文件的统计结果、每个目录(脚本输入参数)的统计结果、不在统计范围的文件的信息。通过修改脚本的几个全局控制变量即可达到此目的。

 

代码如下:

函数实现脚本libaccount.sh

#!/bin/sh
#print flag : default value is to print
#per file
pflag=1
#per dir
gflag=1
#invalid file
invalidflag=1
#default value
flagok=1

#global variable
gblank=0
gcomment=0
gtotal=0
#temp file suffix
tmpsuffix="tmp"

#per file variable
pblank=0
pcomment=0
ptotal=0

function init()
{
	gblank=0
	gcomment=0
	gtotal=0
	tmpsuffix="tmp"
	pblank=0
	pcomment=0
	ptotal=0
}

#print per file info
function printfileinfo()
{
	if [ "$pflag" -eq "$flagok" ]
	then
	echo "filename: "$1
	echo "total lines: "$ptotal
	echo "blank lines: "$pblank
	echo "comment lines: "$pcomment
	fi
}


#input filename(has path)
#account per file info
function accountonefile()
{
	#/*----*/
	#       /*-----*/
	#//
	#     //
	sed "/^[[:blank:]]*\/\*/,/\*\//d" $1 | sed "/^[[:blank:]]*\/\//d" > $1$tmpsuffix
	tsum=`cat $1 | wc -l`
	tnocom=`cat $1$tmpsuffix | wc -l`
	tnoblank=`sed -e "/^[[:blank:]]*$/d" $1$tmpsuffix | wc -l`
	
	ptotal=`expr $tsum`
	pblank=`expr $tnocom - $tnoblank`
	pcomment=`expr $tsum - $tnocom`
	
	rm -rf $1$tmpsuffix	

	printfileinfo $1
}

#only .c .h file is right
function validfile()
{
        pp=`echo $1 | awk -F"." ‘{print $NF}‘`
        if [ "$pp" = "c" -o  "$pp" = "h"  ]
        then
                return 1
        else
                return 0
        fi
}

#print file info which is not in the account scope
function printnotinscope()
{
	if [ "$invalidflag" -eq "$flagok" ]
	then
        echo ""
        echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
        echo ""
        echo "file is not in the account scope   : "$1
        echo ""
        echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
	fi
}

#traverse one dir including its subdirs and files
function loopdir()
{
        for file in `ls $1`
        do
                if [ -f $1"/"$file ]
                then
		validfile $file
		ret=`echo $?`
			if [ "$ret" == "1" ]
			then
				accountonefile $1"/"$file
				gblank=`expr $gblank + $pblank`
				gcomment=`expr $gcomment + $pcomment`
				gtotal=`expr $gtotal + $ptotal`
			else
				printnotinscope $1"/"$file
			fi
                fi
                if [ -d $1"/"$file ]
                then
                        echo ""
                        echo direcoty : $1"/"$file
                        loopdir $1"/"$file
                        echo ""
                fi
        done
}

#print per dir info
function printaccountresult()
{
	if [ "$gflag" -eq "$flagok" ]
	then
	validcode=0
	validcode=`expr $gtotal - $gcomment - $gblank`
	echo ""
	echo "*************$1****************"
	echo ""
	echo "total code lines    : "$gtotal
	echo "total blank lines   : "$gblank
	echo "total comment lines : "$gcomment
	echo "total valid lines   : "$validcode
	echo ""
	echo "*************$1***************"
	fi
}

可运行脚本account.sh

#!/bin/sh
. libaccount.sh
#account all the dirs
allblank=0
alltotal=0
allcomment=0

#whether to print according info or not

#okflag : print
#notokflag : not to print
okflag=1
notokflag=0
#per file info
pflag=$notokflag
#per dir info
gflag=$okflag
#invalid file info
invalidflag=$notokflag

function printlastresult()
{
        allvalidcode=0
        allvalidcode=`expr $alltotal - $allcomment - $allblank`
        echo ""
        echo "**************sum account************"
        echo ""
        echo "total code lines    : "$alltotal
        echo "total blank lines   : "$allblank
        echo "total comment lines : "$allcomment
        echo "total valid lines   : "$allvalidcode
        echo ""
        echo "**************sum account************"
}

#init global param for dir account
init

#traverse all the dirs
until [ $# -eq 0 ]
do
#travers the param
loopdir $1
#print per dir result
printaccountresult $1

allblank=`expr $allblank + $gblank`
alltotal=`expr $alltotal + $gtotal`
allcomment=`expr $allcomment + $gcomment`

init
#turn to next param
shift

done
#print the account result
printlastresult


 

代码行统计脚本,布布扣,bubuko.com

代码行统计脚本

原文:http://blog.csdn.net/pngynghay/article/details/21443199

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!