统计一个目录下的文件数

#!/bin/bash
#******************
#统计一个目录下的文件,并根据文件的拥有者统计出每个用户的文件个数,并采用进度条的方式显示出结果
#*copyleft maisui2016-03-13
#*version 1
if [ $# != 1 ]
then 
	echo 
	echo "count how many file belong to which user:"
	echo "Usage:"
	echo -e "\t `basename $0` [directory]"
	echo
	exit 1
fi
ls -lR "$1" | awk '
	function max(arr,i,max_number){
		max_number=0
		for ( i in arr ){
			max_number=arr[i];
		}
		return max_number
	}
	function progress_length(currentlength,maxlength)
	{
		return currentlength/maxlength*50	
	}
	BEGIN {
		printf "%-10s %8s\n","username","filenumbers";
	}
	/^-/{
	result[$3]++
	}
	END {
		max_length=max(result);
		for (user in result)
			{
				printf "%-10s[%8d]: ",user,result[user];
				for (i=1;i<progress_length(result[user],max_length);i++)
				{
					printf "######"
				}
		}	printf "\n";
	}
	'
exit 0

awk语言遍历数组中的所有元素用的for语句 for(i in array)
打印进度条时,需要用文件个数最多的用户为基准来显示进度条,首先在END中调用函数max获得进度条,max函数接受两个参数,第一个参数arr代表了调用时传进的数组名,另外两个不是参数,而是max函数的本地变量,变量i在遍历数组时,连续地保存了索引值,而变量max_number用来记录数组元素中最大值

posted @ 2016-07-26 16:55  夏日花开  阅读(297)  评论(0编辑  收藏  举报