原创不易,转载请注明出处,谢谢。
CodeCounter.sh
#! /bin/bash
#=========================================================================
# @usage
# @brief 统计代码行数信息
# @author Lynn
# @version 1.00 2017/12/02
#=========================================================================
declare -a ignoreDirArr=(DB include lib) #不参与代码统计的文件目录
declare srcFileType=".cpp" #源文件后缀名
declare headFileType=".hpp" #头文件后缀名
declare folderPath #要统计的文件的路径
declare codeLineCount #代码行数
declare commentLineCnt #注释行数
declare srcFileCount #源文件个数
declare headFileCount #头文件个数
# @brief 统计注释行数函数
# @param N/A
# @return N/A
countCommentsRowsCnt()
{
#1统计源文件注释行数
commentLineCnt=`find $folderPath \( -name ${ignoreDirArr[0]} -o \
-name ${ignoreDirArr[1]} -o \
-name ${ignoreDirArr[2]} \) -prune -o -name *$srcFileType -print \
| xargs cat | egrep -e \/\/ -e ^[\ \ ]*\[*] -e \/\[*] | wc -l`
#2统计头文件注释行数
(( commentLineCnt+=`find $folderPath \( -name ${ignoreDirArr[0]} \
-o -name ${ignoreDirArr[1]} -o -name \
${ignoreDirArr[2]} \) -prune -o -name *$headFileType -print \
| xargs cat | egrep -e \/\/ -e ^[\ \ ]*\[*] -e \/\[*] | wc -l` ))
}
# @brief 统计有效代码行数函数
# @param N/A
# @return N/A
countCodeRowsCnt()
{
#1统计源文件有效代码行数
codeLineCount=`find $folderPath \( -name ${ignoreDirArr[0]} -o \
-name ${ignoreDirArr[1]} -o \
-name ${ignoreDirArr[2]} \) -prune -o -name *$srcFileType -print \
| xargs cat | egrep -v -e ^[\ \ ]*$ -e ^[\ \ ]*\/ -e ^[\ \ ]*\[*] | wc -l`
#2统计头文件有效代码行数
(( codeLineCount+=`find $folderPath \( -name ${ignoreDirArr[0]} \
-o -name ${ignoreDirArr[1]} -o -name \
${ignoreDirArr[2]} \) -prune -o -name *$headFileType -print \
| xargs cat | egrep -v -e ^[\ \ ]*$ -e ^[\ \ ]*\/ -e ^[\ \ ]*\[*] | wc -l` ))
}
runSouceCounter()
{
#>>>----------------------------------------------------------------------------
#1.参数防呆
#1.1参数个数防呆
if [[ $# -eq 0 ]]; then
echo "请输入当前目录下的一个目录名作为第一位置参数"
exit 1
elif [[ $# -gt 1 ]]; then
echo "输入了多余的位置参数,只使用了第一位置参数作为目录名"
fi
#1.2参数内容防呆
folderPath=$1
test -d $folderPath
if [[ $? != 0 ]]; then
echo "不存在目录$_,请输入当前目录下的一个目录名作为第一位置参数"
exit 1
fi
#>>>----------------------------------------------------------------------------
# 2.统计文件个数:
# 2.1源文件文件个数
srcFileCount=`find $folderPath \( -name ${ignoreDirArr[0]} -o \
-name ${ignoreDirArr[1]} -o -name ${ignoreDirArr[2]} \
\) -prune -o -name *$srcFileType -print | wc -l`
# 2.2头文件文件个数
headFileCount=`find $folderPath \( -name ${ignoreDirArr[0]} -o \
-name ${ignoreDirArr[1]} -o -name ${ignoreDirArr[2]} \
\) -prune -o -name *$headFileType -print | wc -l`
if [[ (( `expr $srcFileCount + $headFileCount` -eq 0 )) ]]; then
echo "目录$folderPath中不存在以$srcFileType或$headFileType后缀结尾的代码文件"
exit 0
else
echo "源文件个数为:$srcFileCount个"
echo "头文件个数为:$headFileCount个"
fi
#>>>----------------------------------------------------------------------------
# 3.统计注释行数:
countCommentsRowsCnt #调用统计注释行数函数
echo "注释行数为:$commentLineCnt"
#>>>----------------------------------------------------------------------------
# 4.统计有效代码行数:
countCodeRowsCnt #调用统计有效代码行数函数
echo "有效代码行数为:$codeLineCount行"
}
runSouceCounter $@