一個統計文件夾代碼行數的工具

臨時起意想看一下自己到目前爲止寫了多少行代碼了,於是用shell腳本寫了一個比較方便的統計工具。代碼很簡單,有需求的讀者可根據自己的需求做一些修改。

#!/bin/bash

#######################################################
#作用:
#  本腳本的作用在於統計指定文件夾內,指定類型代碼的行數
#注:
#  本腳本目前只支持Linux平臺
#選項:
#  第一個選項爲需要統計的文件夾路徑
#    -如果本腳本和要統計的文件夾在統一工作目錄,則可以使用相對路徑
#    -否則務必使用絕對路徑
#  第二個選項爲需要統計的代碼類型,有以下類型:
#    -c:   C語言源文件代碼
#    -cc:  C++語言源文件代碼
#    -h:   C/C++語言頭文件代碼
#    -p:   Python語言代碼
#    -g:   Go語言代碼
#    -j:   Java語言代碼
#
#作者:aideny
#######################################################


path=$1
code_type=$2

option=""

if test ${code_type} = "-c"
then 
  option="*.c"
elif test ${code_type} = "-cc"
then
  option="*.cc"
elif test ${code_type} = "-h"
then
  option="*.h"
elif test ${code_type} = "-p"
then
  option="*.py"
elif test ${code_type} = "-g"
then
  option="*.go"
elif test ${code_type} = "-j"
then
  option="*.java"
else 
  echo "檢查選項是否支持!"
  exit 1
fi

echo "開始統計..."

# 進行統計
if test -z ${path}
then
  echo "Usage: ./cl.sh [filepath][option]"
else 
  # 這個方法如果找不到指定語言的代碼,會卡住
  # wc -l `find ${path} -name ${option}` | tail -n1
  find ${path} -name ${option} | xargs wc -l | tail -n1
fi


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章