天天看點

統計檔案的行數

工作也有一年多了,寫的代碼到底有多少行數,突然想統計下

研究了下shell,做了一個腳本

在mac os 10.9上通過

ps:wc -l在mac上統計會少一行,是以結果處+1,其他系統沒測試

whichDir=$1
whichFile=$2
if [[ -z "$1" ]]; then
    echo "please input dir"
    exit -1
fi

if [[ ! -d "$1" ]]; then
    echo "$1 is not a dir"
    exit -1
fi

if [[ -z "$2" ]]; then
    echo "please input file type"
    exit -1
fi


declare -i count
count=0
function go_count() {
    for innerfile in $1/*
    do
        if [ -d "$innerfile" ]; then
            go_count "$innerfile" 
        elif [[ -f "$innerfile" && "${innerfile##*.}" = "${whichFile}" ]]; then
            declare -i count2="$(wc -l "${innerfile}" | awk '{print $1}')"
            let count=count+count2+1
        fi
    done
}
go_count "$whichDir"
echo "in ${whichDir} has file typed ${whichFile} count=${count}"
           

繼續閱讀