代码行统计脚本

来源:互联网 发布:重庆公务员培训 知乎 编辑:程序博客网 时间:2024/06/05 05:40

功能

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

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

 

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

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

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

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

4、统计文件总行数

5、可输入多个目录

 

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

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

 

代码如下:

函数实现脚本libaccount.sh

#!/bin/sh#print flag : default value is to print#per filepflag=1#per dirgflag=1#invalid fileinvalidflag=1#default valueflagok=1#global variablegblank=0gcomment=0gtotal=0#temp file suffixtmpsuffix="tmp"#per file variablepblank=0pcomment=0ptotal=0function init(){gblank=0gcomment=0gtotal=0tmpsuffix="tmp"pblank=0pcomment=0ptotal=0}#print per file infofunction printfileinfo(){if [ "$pflag" -eq "$flagok" ]thenecho "filename: "$1echo "total lines: "$ptotalecho "blank lines: "$pblankecho "comment lines: "$pcommentfi}#input filename(has path)#account per file infofunction accountonefile(){#/*----*/#       /*-----*/#//#     //sed "/^[[:blank:]]*\/\*/,/\*\//d" $1 | sed "/^[[:blank:]]*\/\//d" > $1$tmpsuffixtsum=`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$tmpsuffixprintfileinfo $1}#only .c .h file is rightfunction 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 scopefunction 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 filesfunction loopdir(){        for file in `ls $1`        do                if [ -f $1"/"$file ]                thenvalidfile $fileret=`echo $?`if [ "$ret" == "1" ]thenaccountonefile $1"/"$filegblank=`expr $gblank + $pblank`gcomment=`expr $gcomment + $pcomment`gtotal=`expr $gtotal + $ptotal`elseprintnotinscope $1"/"$filefi                fi                if [ -d $1"/"$file ]                then                        echo ""                        echo direcoty : $1"/"$file                        loopdir $1"/"$file                        echo ""                fi        done}#print per dir infofunction printaccountresult(){if [ "$gflag" -eq "$flagok" ]thenvalidcode=0validcode=`expr $gtotal - $gcomment - $gblank`echo ""echo "*************$1****************"echo ""echo "total code lines    : "$gtotalecho "total blank lines   : "$gblankecho "total comment lines : "$gcommentecho "total valid lines   : "$validcodeecho ""echo "*************$1***************"fi}

可运行脚本account.sh

#!/bin/sh. libaccount.sh#account all the dirsallblank=0alltotal=0allcomment=0#whether to print according info or not#okflag : print#notokflag : not to printokflag=1notokflag=0#per file infopflag=$notokflag#per dir infogflag=$okflag#invalid file infoinvalidflag=$notokflagfunction 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 accountinit#traverse all the dirsuntil [ $# -eq 0 ]do#travers the paramloopdir $1#print per dir resultprintaccountresult $1allblank=`expr $allblank + $gblank`alltotal=`expr $alltotal + $gtotal`allcomment=`expr $allcomment + $gcomment`init#turn to next paramshiftdone#print the account resultprintlastresult


 

1 1
原创粉丝点击