好早之前写的shell脚本了,统计代码行数用的

原创不易,转载请注明出处,谢谢。

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 $@

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值