当前位置:网站首页>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的乐趣也在于此吧,哈哈去计算项目源码有多大了~
边栏推荐
- 接收用户输入,身高BMI体重指数检测小业务入门案例
- 从零开始匹配vim(0)——vimscript 简介
- 阿里云中mysql数据库被攻击了,最终数据找回来了
- 【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
- golang中的atomic,以及CAS操作
- 交叉验证如何防止过拟合
- Installation and testing of pyflink
- Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe
- 405 method not allowed appears when the third party jumps to the website
- THREE. AxesHelper is not a constructor
猜你喜欢

Installation of gazebo & connection with ROS

ClickHouse字段分组聚合、按照任意时间段粒度查询SQL

boot - prometheus-push gateway 使用

身体质量指数程序,入门写死的小程序项目

Dark horse notes - exception handling

Make Jar, Not War

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

【信号与系统】

Lldp compatible CDP function configuration

1123. The nearest common ancestor of the deepest leaf node
随机推荐
golang中的Mutex原理解析
Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe
Dark horse notes - exception handling
How to prevent overfitting in cross validation
THREE.AxesHelper is not a constructor
Gazebo的安装&与ROS的连接
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
让我们,从头到尾,通透网络I/O模型
MySQL中回表的代价
LeetCode:1175. 质数排列
如何管理分布式团队?
斗地主游戏的案例开发
Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
AI automatically generates annotation documents from code
Google发布安全更新,修复Chrome中已被利用的0 day
Implementation principle of waitgroup in golang
Case development of landlord fighting game
What does security capability mean? What are the protection capabilities of different levels of ISO?
Lldp compatible CDP function configuration
Gnet: notes on the use of a lightweight and high-performance go network framework