使用方法
1. 在自己的linux机器上新建一个wrodcount.sh,然后将下方源码copy进去
2. 执行脚本命令,然后第一个参数是要统计的后缀名,后面跟要统计的所有路径(相对绝对路径都可以)
使用方法样例
./wordcount.sh 'php' '../Tool/' '../Application/'
由于输出内容比较多,建议将输出重定向到某个文件,再查看
(不懂重定向的参考这个链接[http://www.runoob.com/linux/linux-shell-io-redirections.html]学习一下)
./wordcount.sh 'php' '../Tool/' '../Application/' > wordcount_result
源码
# wordcount # author: linhongzhao # describe: 脚本用于计算指定文件和指定目录下的所有文件的总行数 # usage: ./wordcount.sh [extension] [path...] # example: ./wordcount.sh 'php' '../Tool/' '../Application/' #!/usr/bin/env bash total_lines=0 path_lines=0 analyse(){ total=0 # 指定后缀名 extension=$1 path=$2 total_file_list=`find ${path} -name "*.${extension}"` for file in ${total_file_list} do echo -e "\tfile: "${file} count=`wc -l ${file}|awk -F ' ' '{print $1}'` echo -e "\t\tline count:"${count} total=`expr ${total} + ${count}` done path_lines=${total} total_lines=`expr ${total} + ${total_lines}` } extension=$1 # 指定目录 for path in $* do if [ ${path} = $1 ] then continue fi echo '#############' echo -e "analyse path:\n\t"${path} analyse ${extension} ${path} echo 'path line count: '${path_lines} done echo "total line count for all path: "${total_lines}