shell 脚本统计工程代码行数

yuyu888 于 2023-03-17 发布
#!/bin/bash
function getdir(){
    totalnum=0
    # 设置第一级目录,只展示该目录下的文件和目录值
    echo_root_dir="/Users/xxx/Documents/work/java/microservice"  
    for element in `ls $1`
    do
        dir_or_file=$1"/"$element
        echo_root_dir_file=$echo_root_dir"/"$element
        if [ -d $dir_or_file ]
        then
            # 不统计taget的目录
            result=$(echo $dir_or_file | grep "target")
            if [[ "$result" == "" ]]
            then
                # 只有一级目录才输出结果
                dirTotalLineNum=`getdir $dir_or_file`
                if [ $echo_root_dir_file == $dir_or_file ]
                then
                    echo $dirTotalLineNum"\t"$dir_or_file >> tj.log
                fi
                totalnum=$(($totalnum+$dirTotalLineNum))
            fi
        else
            # 不统计js文件
            if [ ${dir_or_file##*.} != "js" ]
            then
                fileLineNum=`cat $dir_or_file | wc -l`
                totalnum=$(($totalnum+$fileLineNum))
                if [ $echo_root_dir_file == $dir_or_file ]
                then
                    echo $fileLineNum"\t"$dir_or_file >> tj.log
                fi
            fi
        fi
    done
    echo $totalnum
}

root_dir="/Users/xxx/Documents/work/java/microservice"
getdir $root_dir