1、将密码文件的每一行作为元数赋值给数组
[root@localhost ~]# cat ceshi5.sh
#!/bin/bash
declare -a array
i=0
while read line
do
array[$i]=$line
echo ${array[$i]}
let i++
done </etc/shadow
[root@localhost ~]# sh ceshi5.sh
root:$6$t.SPOmI8ZH6Y3siK$7fWWv65RURuL2Nok0ai9FFJF2ix9TaplvJOMW5R.j8AegqtTqTYL6Uyyka04oAR4bfZGuVP6h85j1E5OtoIBA0::0:99999:7:::
bin:*:18353:0:99999:7:::
daemon:*:18353:0:99999:7:::
adm:*:18353:0:99999:7:::
lp:*:18353:0:99999:7:::
sync:*:18353:0:99999:7:::
shutdown:*:18353:0:99999:7:::
halt:*:18353:0:99999:7:::
mail:*:18353:0:99999:7:::
operator:*:18353:0:99999:7:::
games:*:18353:0:99999:7:::
ftp:*:18353:0:99999:7:::
nobody:*:18353:0:99999:7:::
systemd-network:!!:19435::::::
dbus:!!:19435::::::
polkitd:!!:19435::::::
sshd:!!:19435::::::
postfix:!!:19435::::::
2、使用关联数组统计密码文件中用户使用的不同类型shell的数量
[root@localhost ~]# cat ceshi6.sh
#!/bin/bash
declare -A login
while read line ;do
type=`echo $line |awk -F: '{print $NF}'`
let login[$type]++
done < /etc/passwd
for i in ${!login[*]};do
echo "$i ::::::: ${login[$i]}"
done
[root@localhost ~]# sh ceshi6.sh
/sbin/nologin ::::::: 14
/bin/sync ::::::: 1
/bin/bash ::::::: 1
/sbin/shutdown ::::::: 1
/sbin/halt ::::::: 1
[
3、使用关联数组按扩展名统计指定目录中文件的数量
root@localhost ~]# cat ceshi7.sh
#!/bin/bash
declare -A array
read -p "please input a directory:" pathy
for i in `ls -F $pathy |grep -v /$ |fgrep . | cut -d . -f 2`
do
let array[$i]++
done
for i in ${!array[*]}
do
echo "$i ${array[$i]}"
done
[root@localhost ~]# sh ceshi7.sh
please input a directory:/root
cfg 1
sh 5
[root@localhost ~]# sh ceshi7.sh
please input a directory:/etc
defs 1
png@ 1
db 1
256color 1
conf 25
allow 1
so 2
deny 2
net 1
d@ 8
cnf 1
cshrc 1
lightbgcolor 1
local@ 1
cfg@ 1
login 1