数组
数组
unset array取消定义
普通数组 只能以数字作为索引(下标)
关联数组 可以使用数字也可以使用字符串作为索引(下标)
数组名[索引]=值
索引就是下标
1.普通数组定义方式
第一种定义方式:
数组名[索引]=值
方括号里只能是数字
如果是字符串就会被覆盖 就会错误
[root@shell /server/scripts]# array[0]=shell
[root@shell /server/scripts]# array[1]=linux
[root@shell /server/scripts]# array[2]=python
[root@web02 ~]# array[index1]=Shell
[root@web02 ~]# array[index2]=Linux
[root@web02 ~]# array[index3]=MySQL
[root@web02 ~]# echo ${array[*]}
MySQL
[root@web02 ~]# echo ${array[1]}
空
[root@web02 ~]# echo ${array[2]}
空
[root@web02 ~]# echo ${array[3]}
空
[root@web02 ~]# echo ${array[0]}
MySQL
第二种定义方式:
数组名=(值)
[root@shell /server/scripts]# arraytest=([0]=shell [1]=linux [2]=python)
第三种定义方式:
[root@shell /server/scripts]# array=($(cat /etc/hosts))
2.如何查看某个索引的值
[root@shell /server/scripts]# echo ${array} 默认第一个
shell
[root@shell /server/scripts]# echo ${array[1]}
linux
[root@shell /server/scripts]# echo ${array[2]}
python
`输出全部
[root@shell /server/scripts]# echo ${array[*]}
shell linux python
[root@shell /server/scripts]# echo ${array[@]}
shell linux python
[root@shell /server/scripts]# echo ${arraytest[@]}
shell linux python
`查看索引
[root@shell /server/scripts]# echo ${!array[*]}
0 1 2
[root@shell /server/scripts]# echo ${!array[@]}
0 1 2
[root@shell /server/scripts]# echo ${!arraytest[@]}
0 1 2
索引切片
[root@shell ~]# array[0]=m
[root@shell ~]# array[1]=s
[root@shell ~]# array[2]=y
[root@shell ~]# echo ${array[*]}
m s y
[root@shell ~]# echo ${!array[*]}
0 1 2
[root@shell ~]# echo ${array[*]:1} 索引值1开始全部输出(包括自己)
s y
[root@shell ~]# echo ${array[*]:2} 索引值2开始全部输出(包括自己)
y
[root@shell ~]# echo ${array[*]:0:1} 索引值0开始输出 总共1个 (包括自己)
m
[root@shell ~]# echo ${array[*]:0:2} 索引值0开始输出 总共2个 (包括自己)
m s
[root@shell ~]# echo ${array[*]:2:2}
y
[root@shell ~]# echo ${array[*]:1}
s y
[root@shell ~]# array=([1]=m [2]=s [3]=y)
[root@shell ~]# echo ${array[*]}
m s y
[root@shell ~]# echo ${!array[*]}
1 2 3
[root@shell ~]# echo ${array[*]:1}
m s y
[root@shell ~]# echo ${array[*]:2}
s y
[root@shell ~]# echo ${array[*]:1:2}
m s
[root@shell ~]# echo ${array[*]:2:2}
s y
如果不想获取到数组的下标 只想要内容 则直接循环数组内容
declare -a str1='([0]="good morning" [1]="everyOne")'
for i in ${str1[@]}
do
echo "列表的值为 $i"
done
案例:
[root@web scripts]# cat ip.sh
#!/bin/sh
ip=(
10.0.0.3
10.0.0.6
10.0.0.254
10.0.0.9
)
#for i in ${ip[*]}
#do
# ping -c 1 -W 1 $i
#done
for i in ${!ip[*]}
do
ping -c 1 -W 1 ${ip[$i]}
done
ping通了4个ip
declare -a 查看普通数组
3.关联数组定义方式
关联数组 字符串作为索引
daclare -A 声明一下是关联数组 然后再去定义
[root@shell /server/scripts]# declare -A array
[root@shell /server/scripts]# array[index1]=shell
[root@shell /server/scripts]# array[index2]=linux
[root@shell /server/scripts]# array[index3]=python
[root@shell /server/scripts]# echo ${array[index1]}
shell
[root@shell /server/scripts]# echo ${array[index2]}
linux
[root@shell /server/scripts]# echo ${array[index3]}
python
`输出全部值
[root@shell /server/scripts]# echo ${array[@]}
shell linux python
[root@shell /server/scripts]# echo ${array[*]}
shell linux python
`查看全部索引
[root@shell /server/scripts]# echo ${!array[*]}
index1 index2 index3
[root@shell /server/scripts]# echo ${!array[@]}
index1 index2 index3
4.查看数组的长度
[root@shell /server/scripts]# echo ${#array[*]}
3
遍历数组的三种方式:
1. echo ${array[*]} for循环
2. echo ${!array[*]} 使用索引遍历内容
3. echo ${#array[*]} 索引的个数遍历内容
案例
sex.txt
m
m
f
m
m
f
f
x
[root@shell /server/scripts]# cat sex.sh
#!/bin/bash
declare -A array
for i in `cat sex.txt`
do
let array[$i]++
done
for i in ${!array[*]}
do
echo "$i 出现了 ${array[$i]}次"
done
[root@shell /server/scripts]# sh sex.sh
f 出现了 3次
m 出现了 4次
x 出现了 1次
sex.txt
zs m
ls m
em f
alex m
ld m
oldboy f
bgx f
msy x
[root@shell /server/scripts]# cat sex.sh
#!/bin/bash
declare -A array
while read line
do
let array[`echo $line |awk '{print $2}'`]++
done<sex.txt
for i in ${!array[*]}
do
echo "$i 出现了 ${array[$i]}次"
done
`或者
[root@shell /server/scripts]# cat sex.sh
#!/bin/sh
declare -A array
while read line
do
type=`echo $line|awk '{print $2}'`
let array[$type]++
done<sex.txt
for i in ${!array[*]}
do
echo "$i出现了 ${array[$i]}次"
done
[root@shell /server/scripts]# sh sex.sh
f 出现了 3次
m 出现了 4次
x 出现了 1次
同理统计ip地址
[root@web02 ~]# cat array.sh
#!/bin/sh
declare -A array
while read line
do
type=`echo $line|awk '{print $1}'`
let array[$type]++
done</var/log/nginx/access.log
for i in ${!array[*]}
do
echo "$i出现了 ${array[$i]}次"
done
[root@shell /server/scripts]# cat suoyin3.sh
#!/bin/bash
>acce
declare -A array
while read line
do
IP=`echo $line |awk '{print $1}'`
let array[$IP]++
done < access.log.bak
for i in ${!array[*]}
do
echo "当前IP为:$i 对应出现的次数是: ${array[$i]}" >>acce
done
cat acce|sort -rnk3|column -t
查用户
[root@shell /server/scripts]# cat suoyin.sh
#!/bin/bash
while read line
do
u=$(echo $line |awk -F : '{print $1}')
array1[tmp1++]=$u
done < /etc/passwd
for i in ${!array1[*]}
do
echo "第$((i+1))个用户是 ${array1[$i]}"
done
第1个用户是 root
第2个用户是 bin
第3个用户是 daemon
第4个用户是 adm
第5个用户是 lp
第6个用户是 sync
第7个用户是 shutdown
第8个用户是 halt
查命令
[root@shell /server/scripts]# cat suoyin2.sh
#!/bin/bash
>shell.log (反复执行嘛,执行前清空下)
declare -A shells
while read line
do
Bash=`echo $line |awk -F: '{print $NF}'`
let shells[$Bash]++
done < /etc/passwd
for i in ${!shells[*]}
do
echo "当前shell为:$i 对应出现的次数是: ${shells[$i]}" >>shell.log
done
cat shell.log|sort -rnk3|column -t (格式)
[root@shell /server/scripts]# sh suoyin2.sh
当前shell为:/sbin/nologin 对应出现的次数是: 22
当前shell为:/bin/bash 对应出现的次数是: 9
当前shell为:/sbin/shutdown 对应出现的次数是: 1
当前shell为:/sbin/halt 对应出现的次数是: 1
当前shell为:/bin/sync 对应出现的次数是: 1
取状态
[root@shell /server/scripts]# cat suoyin4.sh
#!/bin/bash
declare -A arrar_state
type=`netstat -ant |awk 'NR>2{print $NF}'` 第一个for就是为第二个做准备 和第二个没关系 只是把第
一个的值放在字典里 等第二次用
for j in $type
do
let arrar_state[$j]++
done
for i in ${!arrar_state[*]}
do
echo "$i 出现了 ${arrar_state[$i]}次"
done
[root@shell /server/scripts]# sh suoyin4.sh
ESTABLISHED 出现了 2次
LISTEN 出现了 6次