当前位置:网站首页>shell脚本快速统计项目代码行数
shell脚本快速统计项目代码行数
2022-07-06 17:51:00 【絔离】
需求
最近突然想看看正在开发的项目一共有多少行代码,但是项目又比较大,手动去计算太麻烦了。就想到了shell脚本,只用给它一个路径,等待脚本统计结果即可。
实现思路
1、获取给定路径下的全部文件
2、判断文件是目录还是普通文件,如果是目录,继续遍历;如果是普通文件,下一步
3、判断文件类型,根据文件后缀名判断即可,如c++源码的后缀名有.h和.cpp
4、计算匹配的文件行数,累加即可
demo
#!/bin/bash
# 定义全局变量,计算总行数
line=0
# function listFiles:获取指定路径下的源码总行数
# $1 全路径
# $2 分隔符,用于显示多级目录的缩进效果
function listFiles()
{
echo "path:$1"
for file in $(ls $1)
do
# 如果是文件夹继续遍历
if [ -d $file ]; then
echo "$file is dir"
listFiles $1/$file " $2"
else
# 此处可以修改为java、py等等想要统计的文件后缀名
if [ ${file##*.} == "h" -o ${file##*.} == "cpp" ];then
echo " $2" "$1/$file line:$(wc -l $1/$file | awk '{print $1}')"
# 累加
let line+=$(wc -l $1/$file | awk '{print $1}')
echo "current line:$line"
fi
fi
done
}
listFiles $1 ""
echo "total line:$line"
效果

验证结果
可以看出,shell脚本计算的.h和.cpp文件行数都是正确的。可以修改文件后缀名去统计不同的源码行数,coding的乐趣也在于此吧,哈哈去计算项目源码有多大了~
边栏推荐
- C language instance_ four
- How to manage distributed teams?
- 云呐|工单管理软件,工单管理软件APP
- Transplant DAC chip mcp4725 to nuc980
- 云呐|工单管理办法,如何开展工单管理
- Failed to successfully launch or connect to a child MSBuild. exe process. Verify that the MSBuild. exe
- Atomic in golang, and cas Operations
- Add the applet "lazycodeloading": "requiredcomponents" in taro,
- [100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
- Implementation principle of waitgroup in golang
猜你喜欢

Yunna | work order management measures, how to carry out work order management

【案例分享】网络环路检测基本功能配置

Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman

Do you understand this patch of the interface control devaxpress WinForms skin editor?
![JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]](/img/40/da56fe6468da64dd37d6b5b0082206.png)
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]

The MySQL database in Alibaba cloud was attacked, and finally the data was found

AI automatically generates annotation documents from code

Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform

405 method not allowed appears when the third party jumps to the website

免费白嫖的图床对比
随机推荐
交叉验证如何防止过拟合
The difference between spin and sleep
C # method of calculating lunar calendar date 2022
JTAG debugging experience of arm bare board debugging
tansig和logsig的差异,为什么BP喜欢用tansig
C语言实例_5
Supersocket 1.6 creates a simple socket server with message length in the header
Add the applet "lazycodeloading": "requiredcomponents" in taro,
搭建【Redis in CentOS7.x】
LeetCode:1175. Prime permutation
1123. 最深叶节点的最近公共祖先
THREE. AxesHelper is not a constructor
Transplant DAC chip mcp4725 to nuc980
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
ESP Arduino (IV) PWM waveform control output
Typical problems of subnet division and super network construction
Spark TPCDS Data Gen
Data type of pytorch tensor
Boot - Prometheus push gateway use
1123. The nearest common ancestor of the deepest leaf node