[root@qls /service/scripts/day12]# cat array-2.sh
#!/bin/bash
#声明关联数组
declare -A Array_Shells
#批量赋值
while read line
do
#赋值
#Type=$(echo $line | awk -F: '{print $NF}')
# /bin/bash 这个是索引 对应的值 次数 数字
let Array_Shells[${line##*:}]++
done < /etc/passwd
#批量打印结果
for i in ${!Array_Shells[*]}
do
echo -e "Shell: $i\t\tCount: ${Array_Shells[$i]}"
done
[root@qls /service/scripts/day12]# sh array-2.sh
Shell: /sbin/nologin Count: 24
Shell: /bin/sync Count: 1
Shell: /bin/bash Count: 23
Shell: /sbin/shutdown Count: 1
Shell: /sbin/halt Count: 1
[root@qls /service/scripts/day12]# wc -l /etc/passwd
50 /etc/passwd
[root@qls /service/scripts/day12]# awk -F: '{print $NF}' /etc/passwd | sort | uniq -c
23 /bin/bash
1 /bin/sync
1 /sbin/halt
24 /sbin/nologin
1 /sbin/shutdown
使用数组取出系统中每一种shell的数量
最新推荐文章于 2022-09-04 22:57:29 发布