shell编程--简单代码统计脚本

来源:互联网 发布:汕头哪里有培训淘宝 编辑:程序博客网 时间:2024/05/29 14:12
#!/bin/bashhelp(){        echo "usage : count.sh search_key search_date"}if [ $# -ne 2 ];then        help        exit 0else        cd ~/source        n=`grep -rn "$1" ./ | grep "$2"|wc -l`        if [ $(($n%2)) -ne 0 ];then                echo "source code tag not match"                exit 0        fi        sum1=`grep -rn "$1" ./ | grep "$2" | awk -F: 'BEGIN {i = 0; sum = 0}{if (NR%2 != 0){i = $2} else {odd = $2; sum += $2 - i + 1}} END {print sum}'`        cd ~/include        n=`grep -rn "$1" ./ | grep "$2"|wc -l`        if [ $(($n%2)) != 0 ];then                echo "include code tag not match"                exit 0        fi        sum2=`grep -rn "$1" ./ | grep "$2" | awk -F: 'BEGIN {i = 0; sum = 0}{if (NR%2 != 0){i = $2} else {odd = $2; sum += $2 - i + 1}} END {print sum}'`         printf "code line count = %d\n" $(($sum1 + $sum2))fi


注意不同shell在语法上有差异,例子使用bash

 

1.在bash下 $(())可以简单计算字符串的算术运算

2.条件控制语句,注意下面的空格

if空格[空格expression空格];then

fi

 

if空格[空格expression空格];then

    语句

else    #注意else里最少要有一句,否则就不要用else

    语句

fi

 

if空格[空格expression空格];then

    语句

elif空格[空格expression空格];then

    语句 

else    #注意else里最少要有一句,否则就不要用else

    语句

fi

3.变量的赋值,等号两边不能有空格

4.printf 格式字符串 参数1 参数2 ...#注意中间的空格

5.awk中的NR,表示当前已经读取的记录数,从1开始,不是0

0 0
原创粉丝点击