把要统计的对象作为数组的索引
declare -A shells // 关联数组必须提前定义
- while read line
- do
- type=
- echo $line |awk -F":" '{print $NF}'
- let shells[$type]++
- done </etc/passwd
- for i in ${!shells[@]}
- do
- echo "$i : ${shells[$i]}"
- done
关联数组只能按照索引遍历, 而普通数组可以根据个数遍历
bash -n ./count_shells.sh
其实可以一条命令统计 shell 的数量
awk -F":" '{print $NF}' /etc/passwd |sort |uniq -c
来源: http://www.bubuko.com/infodetail-2567002.html