关闭

简单的项目规模统计方法

783人阅读 评论(1) 收藏 举报
因为工作的需要,花了一点点时间做了一个关于项目规模统计的shell脚本,共大家分享。

#/bin/bash

 

do_scale()

{

       echo 'Probe *.h, *.hpp, *.cc, *.c, *.cpp files, please wait ...'

       MY_FILES=`find $1 | egrep -v 'Linux_LIB' | egrep '*/.h$|*/.hpp$|*/.c$|*.cpp$|*/.cc$' | sort`

       echo '...Done!'

       # Clear dumping file

       if [ -f ./scale.dump ]; then

              mv ./scale.dump ./scale.dump.bak

       fi

       printf "Name" >> ./scale.dump

       MY_CHARS=`printf "Name" | wc -m`

       MY_CHARS=$((32-MY_CHARS))

       while [ $MY_CHARS -gt 0 ]; do

              printf " " >> ./scale.dump

              MY_CHARS=$((MY_CHARS-1))

       done

       printf "Lines" >> ./scale.dump

       MY_CHARS=`printf "Lines" | wc -m`

       MY_CHARS=$((32-MY_CHARS))

       while [ $MY_CHARS -gt 0 ]; do

              printf " " >> ./scale.dump

              MY_CHARS=$((MY_CHARS-1))

       done

       printf "Whole Path/n" >> ./scale.dump

       set -- $MY_FILES

       MY_TOTAL_FILE_NUMS=$#

       MY_TOTAL_FILE_LINE_NUMS=0

       while [ $# -gt 0 ]; do

              MY_FILE_LINE_NUMS=`cat $1 | wc -l`

              echo "$1: $MY_FILE_LINE_NUMS lines"

              MY_TOTAL_FILE_LINE_NUMS=$((MY_TOTAL_FILE_LINE_NUMS+MY_FILE_LINE_NUMS))

              MY_FILE_LINE_NUMS=`echo $MY_FILE_LINE_NUMS | tr -d ' '`

              # Dump it

              MY_FILE_NAME=`basename $1`

              printf "$MY_FILE_NAME" >> ./scale.dump

              MY_CHARS=`printf "$MY_FILE_NAME" | wc -m`

              MY_CHARS=$((32-MY_CHARS))

              while [ $MY_CHARS -gt 0 ]; do

                     printf " " >> ./scale.dump

                     MY_CHARS=$((MY_CHARS-1))

              done

              printf "$MY_FILE_LINE_NUMS" >> ./scale.dump

              MY_CHARS=`printf "$MY_FILE_LINE_NUMS" | wc -m`

              MY_CHARS=$((32-MY_CHARS))

              while [ $MY_CHARS -gt 0 ]; do

                     printf " " >> ./scale.dump

                     MY_CHARS=$((MY_CHARS-1))

              done

              printf "$1/n" >> ./scale.dump

              shift 1

       done

       echo "Total Files     : $MY_TOTAL_FILE_NUMS"

       echo "Total file lines: $MY_TOTAL_FILE_LINE_NUMS"

       echo "Total Files     : $MY_TOTAL_FILE_NUMS" >> ./scale.dump

       echo "Total file lines: $MY_TOTAL_FILE_LINE_NUMS" >> ./scale.dump

}

 

do_help()

{

       echo "[Usage]: $0 path"

}

 

# Check and setup path

if [ $# -eq 0 ]; then

       MY_PATH=`pwd`

       echo "No path and use current path: $MY_PATH"

elif [ $# -eq 1 ]; then

       MY_PATH=$1

else

       echo 'Unsupport multi-paths'

       do_help

       exit 1

fi

 

# Check whether path is valid

if [ ! -e $MY_PATH ]; then

       echo "$MY_PATH does not exist"

       exit 2

fi

 

if [ ! -d $MY_PATH ]; then

       echo "$MY_PATH is not directory"

       exit 3

fi

 

# Do scale

do_scale $1

存在的几个问题,不知道如何解决?
1)对于计算字符串的方法,这些代码被重用过多次,所以我希望能够放在一个类似于函数中,只要输入对应不同的参数来控制,但是现在因为$1会被冲掉,所以代码看上去很脏。
2)在linux下面,有什么工具可以识别函数。

请有网友多多帮助,谢谢!

对于上次所说的第一个问题,我在学习bash的帮助后,得到解决,请看下面的代码
#/bin/bash

do_scale()
{
    echo 'Probe *.h, *.hpp, *.cc, *.c, *.cpp files, please wait ...'
    MY_FILES=`find $1 | egrep -v 'Linux_LIB' | egrep '*/.h$|*/.hpp$|*/.c$|*.cpp$|*/.cc$' | sort`
    echo '...Done!'
    # Clear dumping file
    if [ -f ./scale.dump ]; then
        mv ./scale.dump ./scale.dump.bak
    fi
    printf "%-32.32s" 'Name' >> ./scale.dump
    #MY_CHARS=`printf "Name" | wc -m`
    #MY_CHARS=$((32-MY_CHARS))
    #while [ $MY_CHARS -gt 0 ]; do
    #    printf " " >> ./scale.dump
    #    MY_CHARS=$((MY_CHARS-1))
    #done
    printf "%-32.32s" 'Lines' >> ./scale.dump
    #MY_CHARS=`printf "Lines" | wc -m`
    #MY_CHARS=$((32-MY_CHARS))
    #while [ $MY_CHARS -gt 0 ]; do
    #    printf " " >> ./scale.dump
    #    MY_CHARS=$((MY_CHARS-1))
    #done
    printf "Whole Path/n" >> ./scale.dump
    set -- $MY_FILES
    MY_TOTAL_FILE_NUMS=$#
    MY_TOTAL_FILE_LINE_NUMS=0
    while [ $# -gt 0 ]; do
        MY_FILE_LINE_NUMS=`cat $1 | wc -l`
        echo "$1: $MY_FILE_LINE_NUMS lines"
        MY_TOTAL_FILE_LINE_NUMS=$((MY_TOTAL_FILE_LINE_NUMS+MY_FILE_LINE_NUMS))
        MY_FILE_LINE_NUMS=`echo $MY_FILE_LINE_NUMS | tr -d ' '`
        # Dump it
        MY_FILE_NAME=`basename $1`
        printf "%-32.32s" "$MY_FILE_NAME" >> ./scale.dump
        #MY_CHARS=`printf "$MY_FILE_NAME" | wc -m`
        #MY_CHARS=$((32-MY_CHARS))
        #while [ $MY_CHARS -gt 0 ]; do
        #    printf " " >> ./scale.dump
        #    MY_CHARS=$((MY_CHARS-1))
        #done
        printf "%-32.32s" "$MY_FILE_LINE_NUMS" >> ./scale.dump
        #MY_CHARS=`printf "$MY_FILE_LINE_NUMS" | wc -m`
        #MY_CHARS=$((32-MY_CHARS))
        #while [ $MY_CHARS -gt 0 ]; do
        #    printf " " >> ./scale.dump
        #    MY_CHARS=$((MY_CHARS-1))
        #done
        printf "$1/n" >> ./scale.dump
        shift 1
    done
    echo "Total Files     : $MY_TOTAL_FILE_NUMS"
    echo "Total file lines: $MY_TOTAL_FILE_LINE_NUMS"
    echo "Total Files     : $MY_TOTAL_FILE_NUMS" >> ./scale.dump
    echo "Total file lines: $MY_TOTAL_FILE_LINE_NUMS" >> ./scale.dump
}

do_help()
{
    echo "[Usage]: $0 path"
}

# Check and setup path
if [ $# -eq 0 ]; then
    MY_PATH=`pwd`
    echo "No path and use current path: $MY_PATH"
elif [ $# -eq 1 ]; then
    MY_PATH=$1
else
    echo 'Unsupport multi-paths'
    do_help
    exit 1
fi

# Check whether path is valid
if [ ! -e $MY_PATH ]; then
    echo "$MY_PATH does not exist"
    exit 2
fi

if [ ! -d $MY_PATH ]; then
    echo "$MY_PATH is not directory"
    exit 3
fi

# Do scale
do_scale $1
原来的很脏的代码,现在被简单的被printf的格式控制给替换,速度也提高了很多!"%-32.32s"重的‘-’表示左对齐,如果去掉‘-’,则为右对齐。
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:547292次
    • 积分:7602
    • 等级:
    • 排名:第2908名
    • 原创:158篇
    • 转载:109篇
    • 译文:0篇
    • 评论:353条
    文章分类
    最新评论
    eBook
    Linux GNU
    Linux GUI
    电子元器件网站
    金融网站
    开源网站
    垮平台GUI
    网页脚本教程