LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1492|回复: 10

我写了个统计源码的脚本,各位帮我完善下吧

[复制链接]
发表于 2007-12-26 15:19:49 | 显示全部楼层 |阅读模式
  1. #!/bin/bash

  2. base=$(pwd)

  3. f()
  4. {
  5. w=0
  6. for s in $( ls $1 )
  7. do
  8.         st=${s%/*}
  9.         if [ -d $1/$st ]
  10.         then
  11.                 t=$(f $1/$st)
  12.                 let "w=w+t"
  13.         else
  14.                 t=$(wc -l $1/$st | awk '{print $1}')
  15.                 let "w=w+t"
  16.         fi
  17. done

  18. echo $w

  19. }

  20. f $base
复制代码

在源码根目录下./***运行,想递归遍历目录统计*.h和*.c的行数。想了好久想不出来,只写出统计所有文件的行数总和。
希望有人能完善下,使其只分析*.h和*.c
重新写也行,我也当抛砖引玉吧。
发表于 2007-12-26 15:58:02 | 显示全部楼层
count.sh
  1. #!/bin/bash
  2. tc=0
  3. th=0
  4. c=0
  5. h=0
  6. echo -n "analysis $1 ..."
  7. find "$1" -type f -name "*.[ch]" |(
  8. while read file;do
  9.         l=$(wc -l < "$file")
  10.         case $file in
  11.                 *.c)
  12.                         ((c++))
  13.                         ((tc+=l))
  14.                         ;;
  15.                 *.h)
  16.                         ((h++))
  17.                         ((th+=l))
  18.                         ;;
  19.         esac
  20. done
  21. echo "done!"
  22. echo "*.c total lines:$tc [$c files]"
  23. echo "*.h total lines:$th [$h files]"
  24. )
复制代码
chmod +x count.sh

./count.sh 源码目录
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-12-26 16:04:13 | 显示全部楼层
#!/bin/bash
w=0
for s in $( find . -name *.[hc] )
do
        t=$( wc -l $s | awk '{print $1}' )
        let "w=w+t"
done
echo $w
++++++++++++++++++++++++
hehe,好好
再次谢谢!
我后来也写了个
回复 支持 反对

使用道具 举报

发表于 2007-12-26 16:28:59 | 显示全部楼层
这个是用递归函数
count.sh
  1. #!/bin/bash
  2. tc=0
  3. th=0
  4. c=0
  5. h=0
  6. function countdir(){
  7.         local file=""
  8.         for file in $(ls "$1");do
  9.                 if [ -d "$1/$file" ];then
  10.                         countdir "$1/$file"
  11.                 elif [ -f "$1/$file" ];then
  12.                         case $file in
  13.                                 *.c)
  14.                                         ((c++))
  15.                                         ((tc+=$(wc -l <"$1/$file")))
  16.                                         ;;
  17.                                 *.h)
  18.                                         ((h++))
  19.                                         ((th+=$(wc -l <"$1/$file")))
  20.                                         ;;
  21.                         esac
  22.                 fi
  23.         done
  24. }
  25. echo -n "analysis $1 ..."
  26. countdir "$1"
  27. echo "done!"
  28. echo "*.c total lines:$tc [$c files]"
  29. echo "*.h total lines:$th [$h files]"
复制代码
chmod +x count.sh
./count.sh 源码目录
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-12-26 20:26:06 | 显示全部楼层
  1. #!/bin/bash
  2. f()
  3. {
  4. w=0
  5. for s in $( ls $1 )
  6. do
  7.         if [ -f $1 ]
  8.         then
  9.                 let "w+=$(wc -l <$1)"
  10.                 break
  11.         fi
  12.         st=${s%/*}
  13.         if [ -d $1/$st ]
  14.         then
  15.                 let "w+=$(f $1/$st)"
  16.         else
  17.                 case $st in
  18.                         *.c|*.h)
  19.                                 let "w+=$(wc -l <"$1/$st")"
  20.                                 ;;
  21.                 esac
  22.         fi
  23. done
  24. echo $w
  25. }
  26. if [ -z "$1" ]
  27. then
  28.         echo "Uasge:count_code path!"
  29.         exit 1
  30. else
  31.         f $1
  32. fi
复制代码
在springwind兄的提示下,完善了这个脚本,+x放到/usr/bin下,输入文件或目录(支持.及..)就可显示*.h和*.c的总行数。
回复 支持 反对

使用道具 举报

发表于 2007-12-30 06:18:54 | 显示全部楼层
  1. find . -name "*.c" -exec wc -l {} \;| awk 'sum+=$1;END{print sum}' | tail -1
复制代码
change '.c' to '.h' to count the .h file lines.  Combination of shell commands can to incredible things.
回复 支持 反对

使用道具 举报

发表于 2008-1-20 15:55:59 | 显示全部楼层
问:
我把代码运行了一下,想统计当前目录下的*.c 和*.h文件,可是结果都是0
命令行输入count.sh ./正确吗?
回复 支持 反对

使用道具 举报

发表于 2008-1-20 15:59:34 | 显示全部楼层
[QUOTE=lxsbupt;1809219]问:
我把代码运行了一下,想统计当前目录下的*.c 和*.h文件,可是结果都是0
命令行输入count.sh ./正确吗?

当前目录下文件和运行结果如下:

[root@localhost zuoye]# ls
input.txt  makefile  rand.c       statistic        sub.cc  tt
main.cc    mkfile    shellscript  statistic_lines  sub.h
[root@localhost zuoye]# ./statistic_lines ./*
Analyse ./input.txt ......
*.c total lines:0
*.h total lines:0
*.c numbers:0
*.h numbers:0
回复 支持 反对

使用道具 举报

发表于 2008-1-20 16:08:46 | 显示全部楼层
问题解决了,最后那个括号位置打错了。。。
回复 支持 反对

使用道具 举报

发表于 2008-1-20 16:15:29 | 显示全部楼层
我在脚本的最后一行加入了
echo "tc=$tc"
为什么最后一行运行输出是:tc=0,而不是上面统计出来的值,why???
刚学shell编程,请多多指教。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表