代码行统计脚本

本文介绍了一个用于统计代码行数的Shell脚本,包括注释行、空行、有效代码行及文件总数的计算。脚本支持多目录输入,并能输出指定格式的统计结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

功能

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

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

 

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

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

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

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

4、统计文件总行数

5、可输入多个目录

 

在代码中,对所统计的文件类型做了限制,默认只统计".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


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值