• 统计一个目录下的文件数


    #!/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 "	 `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
    ","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 "
    ";
    	}
    	'
    exit 0
    

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

  • 相关阅读:
    Java compiler level does not match the version of the installed Java Project facet.
    Project configuration is not up-to-date with pom.xml. Select: Maven->Update Project... from the project context menu or use Quick Fix.
    JavaScript
    JavaScript
    Eclipse
    Eclipse
    Oracle
    Java
    Ext JS 4.2
    Eclipse
  • 原文地址:https://www.cnblogs.com/hanfei-1005/p/5708088.html
Copyright © 2020-2023  润新知