使用方法
脚本指定了惟一一个参数,该参数可以是某个文件的路径或者某个文件夹的路径
./codenum.sh file/dir
思路
源码
#!/bin/bash # 代码统计工具 # 使用: script file/dir # author luodongseu echo "------------------------Welcome!----------------------" #传入的参数 #文件路径 FILE_PATH=$1 #全局变量 #统计对象类型: 1为文件;2为目录 typeset FILE_TYPE=1 #存储行数 typeset TOTAL_LINES=0 #存储文件数量 typeset TOTAL_FILE_NUM=0 #空行数量 typeset TOTAL_BLANK_LINES=0 #注释行数量 typeset TOTAL_COMMENT_LINES=0 #有效代码行数 typeset TOTAL_CODE_LINES=0 #日志文件 LOG_FILE=$(cd $(dirname $0);pwd)/calnum.out echo "" > ${LOG_FILE} ######################################################## # 打印日志函数 ######################################################## function log { echo "[`date +"%Y-%m-%d %H:%M:%S"`]" "[$1]" $2 echo "[`date +"%Y-%m-%d %H:%M:%S"`]" "[$1]" $2 >> ${LOG_FILE} } ######################################################## # 判断统计对象时文件还是目录 ######################################################## function check_is_dir { if [ -f ${FILE_PATH} ];then # 是一个文件 FILE_TYPE=1 elif [ -d ${FILE_PATH} ];then # 是一个目录 FILE_TYPE=2 else # 不存在文件或目录 log "EORROR" "File or dir is not exist!" exit 1 fi } ######################################################## # 统计总行号 ######################################################## function cal_total_num { typeset TMP_TYPE=$1 typeset TMP_PATH=$2 typeset CUR_DIR="./" if [ ${TMP_TYPE} -eq 1 ];then # 单个文件统计 ## 统计文件数量 TOTAL_FILE_NUM=$(expr ${TOTAL_FILE_NUM} + 1) ## 统计行数 typeset current_file_lines=$(cat ${TMP_PATH} | grep "" | wc -l) TOTAL_LINES=$(expr ${TOTAL_LINES} + ${current_file_lines}) log "DEBUG" "File ${TMP_PATH} LINES is ${current_file_lines}." #log "DEBUG" "Current TOTAL_LINES is ${TOTAL_LINES}." ## 统计空白行 typeset current_blank_lines=$(cat ${TMP_PATH} | grep -rn "^\s*$" | wc -l) TOTAL_BLANK_LINES=$(expr ${TOTAL_BLANK_LINES} + ${current_blank_lines}) log "DEBUG" "File ${TMP_PATH} BLANK_LINES is ${current_blank_lines}." #log "DEBUG" "Current TOTAL_BLANK_LINES is ${TOTAL_BLANK_LINES}." ## 统计注释行 typeset current_comment_lines=$(cat ${TMP_PATH} | grep -rn "^\s*#.*$" | wc -l) TOTAL_COMMENT_LINES=$(expr ${TOTAL_COMMENT_LINES} + ${current_comment_lines}) log "DEBUG" "File ${TMP_PATH} COMMENT_LINES is ${current_comment_lines}." #log "DEBUG" "Current TOTAL_COMMENT_LINES is ${TOTAL_COMMENT_LINES}." ## 统计有效代码行 typeset current_code_lines=$(expr ${current_file_lines} - ${current_blank_lines} - ${current_comment_lines}) TOTAL_CODE_LINES=$(expr ${TOTAL_CODE_LINES} + ${current_code_lines}) log "DEBUG" "File ${TMP_PATH} CODE_LINES is ${current_code_lines}." #log "DEBUG" "Current TOTAL_CODE_LINES is ${TOTAL_CODE_LINES}." else # 目录递归遍历 for line in $(ls ${TMP_PATH}) do if [ -f ${TMP_PATH}/$line ];then # 文件 cal_total_num 1 ${TMP_PATH}/$line else # 目录 cal_total_num 2 ${TMP_PATH}/$line fi done fi } ######################################################## # 输出最终的行数结果 ######################################################## function output_lines { printf "%-20s%-30s%-20s%-40s\n" "Step" "Name" "Value" "Comments" printf "%-20s%-30s%-20s%-40s\n" "RESULT" "TOTAL_FILE_NUM" ${TOTAL_FILE_NUM} "#文件总数" printf "%-20s%-30s%-20s%-40s\n" "RESULT" "TOTAL_LINES" ${TOTAL_LINES} "#所有行和" printf "%-20s%-30s%-20s%-40s\n" "RESULT" "TOTAL_BLANK_LINES" ${TOTAL_BLANK_LINES} "#空白行和" printf "%-20s%-30s%-20s%-40s\n" "RESULT" "TOTAL_COMMENT_LINES" ${TOTAL_COMMENT_LINES} "#注释行和" printf "%-20s%-30s%-20s%-40s\n" "RESULT" "TOTAL_CODE_LINES" ${TOTAL_CODE_LINES} "#代码行和" # 输出到日志文件 printf "%-20s%-30s%-20s%-40s\n" "Step" "Name" "Value" "Comments" >> ${LOG_FILE} printf "%-20s%-30s%-20s%-40s\n" "RESULT" "TOTAL_FILE_NUM" ${TOTAL_FILE_NUM} "#文件总数" >> ${LOG_FILE} printf "%-20s%-30s%-20s%-40s\n" "RESULT" "TOTAL_LINES" ${TOTAL_LINES} "#所有行和" >> ${LOG_FILE} printf "%-20s%-30s%-20s%-40s\n" "RESULT" "TOTAL_BLANK_LINES" ${TOTAL_BLANK_LINES} "#空白行和" >> ${LOG_FILE} printf "%-20s%-30s%-20s%-40s\n" "RESULT" "TOTAL_COMMENT_LINES" ${TOTAL_COMMENT_LINES} "#注释行和" >> ${LOG_FILE} printf "%-20s%-30s%-20s%-40s\n" "RESULT" "TOTAL_CODE_LINES" ${TOTAL_CODE_LINES} "#代码行和" >> ${LOG_FILE} } check_is_dir cal_total_num ${FILE_TYPE} ${FILE_PATH} output_lines echo "------------------------End----------------------"