全部博文(2759)
分类: LINUX
2014-01-08 04:33:24
原文地址:统计代码注释率 作者:UnixShapping
#!/bin/bash
#program:
# 分析.c,.cpp文件,计算文件行总数,代码行总数,注释行总数,空白行总数,代码注释率
# 注释类型:
# (1)行注释"//这里是注释内容"
# (2)块注释"/*这里是注释内容*/"
#History:
#2013/12/16 Fgx First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#fileName="/home/fgx/hello_new.c"
echo "-----------------------">> result.txt
date >>result.txt
echo "统计的文件名:">>result.txt
echo "$fileName" >>result.txt
echo >>result.txt
echo "----===统计结果===----">>result.txt
declare -i fileLine=`cat $fileName | wc -l`
echo "文件行总数:"$fileLine"" >>result.txt
declare -i codeLine=`cat $fileName | grep -v '^[[:space:]]*$'|grep -v '^[[:space:]]*\/\/' |sed '/\/\*/,/\*\//d' | wc -l`
echo "代码行总数:"$codeLine"" >>result.txt
declare -i noNoteLine=`cat $fileName | sed '/\/\*/,/\*\//d' | sed '/\/\//d'|wc -l`
declare -i noteLine=0
((noteLine=fileLine-noNoteLine))
echo "注释行总数:"$noteLine"" >>result.txt
declare -i blankLine=`cat $fileName | grep '^[[:space:]]*$' |wc -l`
echo "空白行总数:"$blankLine"" >>result.txt
declare -i line=0
((line=noteLine+codeLine))
echo -n "代码注释率:" >>result.txt
awk 'BEGIN{printf "%.2f%\n",('$noteLine'/'$line')*100}' >>result.txt
echo "----==============----" >>result.txt