免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4923 | 回复: 11
上一主题 下一主题

[学习共享] 写了一个统计文件行数的脚本,请求测试反馈 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-07 16:32 |只看该作者 |倒序浏览
这个脚本统计.c, .h,shell, perl,python文件有效行数。-l 可以指定统计深度。哪位有时间请帮忙测试下。速度有点慢!欢迎各种建议!

  1. #!/bin/bash

  2. #
  3. # func: count .c/.h/shell/perl/python files lines
  4. # version: 1.0
  5. # at Tue Aug  7 16:20:50 CST 2012
  6. #


  7. ###############################################################
  8. #### define variables
  9. ###############################################################

  10. # level deepth of recursive
  11. LEVEL=10000
  12. # the work dir
  13. DEST_DIR=.

  14. #XXX_FILES the total numbers  of XXX files
  15. #XXX_LINES the total lines of all the XXX files

  16. C_FILES=
  17. C_LINES=

  18. H_FILES=
  19. H_LINES=

  20. SHELL_FILES=
  21. SHELL_LINES=

  22. PERL_FILES=
  23. PERL_FILES=

  24. PYTHON_FILES=
  25. PYTHON_LINES=

  26. ###############################################################
  27. #### define functions
  28. ###############################################################

  29. usage(){
  30.         echo "`basename $0` dir [-l level]"
  31. }

  32. is_c_file(){
  33.         [ "${1##*\.}" = "c" ]
  34. }
  35. is_h_file(){
  36.         [ "${1##*\.}" = "h" ]
  37. }
  38. is_shell_file(){
  39.         file "$1" | grep -F "shell script" >/dev/null
  40. }
  41. is_perl_file(){
  42.         file "$1" | grep -F "perl script" >/dev/null
  43. }
  44. is_python_file(){
  45.         file "$1" | grep -F "python script" >/dev/null
  46. }
  47. count_c_file(){
  48.                 cat "$@" |
  49.                 sed -r -e '/^[[:space:]]*\/\*/,/\*\/[[:space:]]*$/d' \
  50.                 -e '/^[[:space:]]*\/\//d' \
  51.                 -e '/^[[:space:]]*\/\*/d' \
  52.                 -e '/^[[:space:]]*$/d' |
  53.                 wc -l
  54. }
  55. count_h_file(){
  56.         count_c_file "$@"
  57. }
  58. count_shell_file(){
  59.         cat "$@" |
  60.         sed -re '1s/#/!/' \
  61.             -e '/^[[:space:]]*#/d' \
  62.             -e '/^[[:space:]]*$/d' |
  63.         wc -l
  64. }
  65. count_perl_file(){
  66.         count_shell_file "$@"
  67. }
  68. count_python_file(){
  69.                 cat "$@" |
  70.                 sed -e '1s/#/!/' |
  71.                 sed -r -e '/^[[:space:]]*"""/,/"""[[:space:]]*$/d' \
  72.                        -e '/^[[:space:]]*#/d' \
  73.                        -e '/^[[:space:]]*$/d' |
  74.                 wc -l
  75. }
  76. count_lines() {
  77.         ((cur_deep++ > $LEVEL)) && return
  78.         for file in $(ls)
  79.         do
  80.                 if [ -f "$file" ]
  81.                 then
  82.                         is_c_file $file && {
  83.                                 lines=$(count_c_file $file)
  84.                                 C_LINES=$(($C_LINES + $lines))
  85.                                 C_FILES=$(($C_FILES + 1))
  86.                         }
  87.                         is_h_file $file  && {
  88.                                 lines=$(count_h_file $file)
  89.                                 H_LINES=$(($H_LINES + $lines))
  90.                                 H_FILES=$(($H_FILES + 1))
  91.                         }
  92.                         is_shell_file $file && {
  93.                                 lines=$(count_shell_file $file)
  94.                                 SHELL_LINES=$(($SHELL_LINES + $lines))
  95.                                 SHELL_FILES=$(($SHELL_FILES + 1))
  96.                         }
  97.                         is_perl_file $file && {
  98.                                 lines=$(count_perl_file $file)
  99.                                 PERL_LINES=$(($PERL_LINES + $lines))
  100.                                 PERL_FILES=$(($PERL_FILES + 1))
  101.                         }
  102.                         is_python_file $file && {
  103.                                 lines=$(count_python_file $file)
  104.                                 PYTHON_LINES=$(($PYTHON_LINES + $lines))
  105.                                 PYTHON_FILES=$(($PYTHON_FILES + 1))
  106.                         }
  107.                 elif [ -d "$file" ]
  108.                 then
  109.                         cd $file
  110.                         count_lines
  111.                 fi
  112.         done
  113.         cd ..  && ((cur_deep--))
  114. }

  115. ###################################################################
  116. # main
  117. ###################################################################


  118. # check the arguments
  119. [ $# -lt 1 ] && { # the number of  arguments  at least 2
  120.         usage
  121.         exit 2
  122. }
  123. # prase the arguments
  124. while [ $# -gt 0 ]
  125. do
  126.         case "$1" in
  127.                 -l) # parse the level to recursion
  128.                         if [ -n "$2" ];then
  129.                                 case "$2" in
  130.                                         *[^0-9]*)
  131.                                                 echo "$2 should be integer"
  132.                                                 exit 2
  133.                                                 ;;
  134.                                         *)
  135.                                                 LEVEL=$2
  136.                                                 shift 2
  137.                                                 ;;
  138.                                 esac
  139.                         else
  140.                                 usage
  141.                                 exit 2
  142.                         fi
  143.                         ;;
  144.                 *) # parse the dir to search
  145.                         if [ -e "$1" -a -d "$1" ]
  146.                         then
  147.                                 DEST_DIR=$1
  148.                                 shift
  149.                         else
  150.                                 echo "$1 not exist or not be a dir"
  151.                                 exit 2
  152.                         fi
  153.                         ;;
  154.         esac
  155. done

  156. # count lines
  157. cd $DEST_DIR
  158. count_lines

  159. # print the result
  160. printf "file type\tfile numbers\ttotal lines\n"
  161. printf "%-9s\t%-12d\t%-12d\t\n" ".c" $C_FILES $C_LINES
  162. printf "%-9s\t%-12d\t%-12d\t\n" ".h" $H_FILES $H_LINES
  163. printf "%-9s\t%-12d\t%-12d\t\n" "perl" $PERL_FILES $PERL_LINES
  164. printf "%-9s\t%-12d\t%-12d\t\n" "shell" $SHELL_FILES $SHELL_LINES
  165. printf "%-9s\t%-12d\t%-12d\t\n" "python" $PYTHON_FILES $PYTHON_LINES
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-08-07 16:45 |只看该作者
好长,,。。。仰望。。。
话说有效行数按什么标准判断呢
如果排除注释和空行,只是单行注释就简单多了

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
3 [报告]
发表于 2012-08-07 16:49 |只看该作者
回复 1# ran3guns


    干嘛不用现成的工具,例如 ohcount, cloc

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
4 [报告]
发表于 2012-08-07 16:58 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
5 [报告]
发表于 2012-08-07 17:01 |只看该作者
回复 2# personball


    去掉注释,包括单行和多行。
    去掉空行。
    只想到这么多, 其他的还没想到。
  

论坛徽章:
0
6 [报告]
发表于 2012-08-07 17:05 |只看该作者
回复 3# MMMIX


    不知道有现成的工具,学习了!
    同学入职,leader给留了这么个任务,所以就帮忙写了。不过过不了leader的测试文件(leader不给看测试文件...)!

论坛徽章:
15
2015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-15 17:29:44午马
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉鸡
日期:2014-04-02 12:24:51双子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥猪
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大牛徽章
日期:2013-04-17 11:48:45
7 [报告]
发表于 2012-08-08 09:37 |只看该作者
leader 那么不配合,为何给他做。 让他一边呆着。

论坛徽章:
0
8 [报告]
发表于 2012-08-08 09:53 |只看该作者
这玩意 实际意义不大吧  这种简直就是没事找事闲的蛋疼的测试

论坛徽章:
1
辰龙
日期:2014-05-22 11:38:58
9 [报告]
发表于 2012-08-08 11:16 |只看该作者
不过过不了leader的测试文件(leader不给看测试文件...)!


过不了是指?
文件个数不对?还是行数不对?

论坛徽章:
0
10
发表于 2012-08-10 17:30
回复 9# winway1988


    统计行数不对。文件个数对。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP