2. 将/etc/shadow文件的每一行作为元数赋值给数组
file=/etc/shadow num=`wc -l < $file` for i in `seq $num` do arr[$i]=`head -$i $file | tail -1` done for i in ${arr[*]} do echo $i done
3.使用关联数组统计文件/etc/passwd中用户使用的不同类型shell的数量
declare -A shells while read line do type=`echo $line | cut -d: -f7` let shells[$type]++ done < /etc/passwd for i in ${!shells[*]} do echo "$i : ${shells[$i]}" done
4.使用关联数组按扩展名统计指定目录中文件的数量
declare -A array num=`find /shell/day* -type d | wc -l` for ((i=0;i<$num;i++)) do type=`find /shell/day* -type d | head -$[$i+1] | tail -1` array[$type]=`find $type -type f -print | wc -l` done for i in ${!array[*]} do echo " $i : ${array[$i]}" done