当前位置:网站首页>Shell script quickly counts the number of lines of project code
Shell script quickly counts the number of lines of project code
2022-07-07 01:46:00 【Li Li】
List of articles
demand
Recently, I suddenly want to see how many lines of code there are in the project under development , But the project is relatively large , It's too troublesome to calculate manually . I think of it. shell Script , Just give it a path , Wait for the statistical results of the script .
Realize the idea
1、 Get all the files in the given path
2、 Determine whether the file is a directory or an ordinary file , If it's a catalog , Continue traversing ; If it's a regular document , next step
3、 Determine file type , Judge according to the file suffix , Such as c++ The suffixes of the source code are .h and .cpp
4、 Calculate the number of matching file lines , Add it up
demo
#!/bin/bash
# Define global variables , Calculate the total number
line=0
# function listFiles: Get the total number of source lines under the specified path
# $1 The full path
# $2 Separator , Used to display the indenting effect of multi-level directories
function listFiles()
{
echo "path:$1"
for file in $(ls $1)
do
# If it is a folder, continue to traverse
if [ -d $file ]; then
echo "$file is dir"
listFiles $1/$file " $2"
else
# It can be modified here as java、py Wait for the file suffix you want to count
if [ ${file##*.} == "h" -o ${file##*.} == "cpp" ];then
echo " $2" "$1/$file line:$(wc -l $1/$file | awk '{print $1}')"
# Add up
let line+=$(wc -l $1/$file | awk '{print $1}')
echo "current line:$line"
fi
fi
done
}
listFiles $1 ""
echo "total line:$line"
effect

The verification results 
It can be seen that ,shell Scripted .h and .cpp The number of lines in the file is correct . You can modify the file suffix to count the number of different source lines ,coding The fun of is also here , Haha, calculate how big the source code of the project is ~
边栏推荐
- C语言实例_4
- [chip scheme design] pulse oximeter
- 我如何编码8个小时而不会感到疲倦。
- Appium foundation - appium inspector positioning tool (I)
- How to prevent overfitting in cross validation
- 设置Wordpress伪静态连接(无宝塔)
- 7.6 simulation summary
- 1123. 最深叶节点的最近公共祖先
- 各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
- Mongodb checks whether the table is imported successfully
猜你喜欢

Comparison of picture beds of free white whoring

今日问题-2022/7/4 lambda体中修改String引用类型变量

鼠标右键 自定义

Blue Bridge Cup 2022 13th provincial competition real topic - block painting

对C语言数组的再认识

Yunna | work order management software, work order management software app

AcWing 345. 牛站 题解(floyd的性质、倍增)

AI automatically generates annotation documents from code

百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (下)

454-百度面经1
随机推荐
Appium automation test foundation uiautomatorviewer positioning tool
制作带照明的DIY焊接排烟器
uva 1401 dp+Trie
修改px4飞控的系统时间
Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
Use nodejs to determine which projects are packaged + released
Set up [redis in centos7.x]
mysqlbackup 还原特定的表
JS Es5 can also create constants?
454-百度面经1
Gin 入门实战
Dark horse notes - create immutable sets and streams
AcWing 1142. Busy urban problem solving (minimum spanning tree)
golang 基础 —— 数据类型
交叉验证如何防止过拟合
Reptile practice (VI): novel of climbing pen interesting Pavilion
AcWing 344. 观光之旅题解(floyd求无向图的最小环问题)
DS-5/RVDS4.0变量初始化错误
MySQL最基本的SELECT(查询)语句
[signal and system]