一个用shell写的统计目录下统计文件行数的代码,


此代码是用来回忆当初学的shell脚本,很多知识忘了,写这个花了我三个小时的时间

#!/bin/ksh

#count *.c file the num of line
g_csum=0
g_hsum=0

count()
{
# echo "$1,count_line"
file_name="$1"
res_line=`wc -l $file_name | awk '{print $1}'`
echo $res_line
return
# find . -name "*.c" | xargs cat | wc -l
}

statistics()
{
echo "program start running"
cur_line=0
echo $1
# g_sum=`count $1`
if test -f $1

then
cur_line=`count $1`
if [ "${1##*.}" = "c" ]
then
g_csum=$(($g_csum+$cur_line))
elif [ "${1##*.}" = "h" ]
then
g_hsum=$(($g_hsum+$cur_line))
fi
echo $cur_line
elif [ -d $1 ]
then
for file in $1/*
do
statistics $file
done
fi
echo "g_csum" $g_csum
echo "g_hsum" $g_hsum
}

for file in $1

do
statistics $1
done

exit 0




相关内容

    暂无相关文章