1. 数组基本概述
什么是数组
简单来讲:数组其实就是变量的一种,变量只能存储一个值,而数组可以存储多个值
数组的分类
分为两类 普通数组 关联数组
普通数组:只能使用正整数作为数组索引
关联数组:可以使用字符串作为数组索引
2. 数组的基本使用
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
nginx php-fpm mysql
[root@shell /scripts/shell12]
nginx php-fpm mysql
[root@shell /scripts/shell12]
0 1 2
[root@shell /scripts/shell12]
0 1 5 10
[root@shell /scripts/shell12]
0 1 2 3 4 5 6 7 8 9
[root@shell /scripts/shell12]
3
[root@shell /scripts/shell12]
10
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
nginx
[root@shell /scripts/shell12]
php-fpm
[root@shell /scripts/shell12]
mysql
[root@shell /scripts/shell12]
php-fpm mysql
[root@shell /scripts/shell12]
nginx php-fpm mysql
[root@shell /scripts/shell12]
nginx
[root@shell /scripts/shell12]
nginx php-fpm
[root@shell /scripts/shell12]
nginx php-fpm mysql
[root@shell ~]
[root@shell ~]
[root@shell ~]
[root@shell ~]
[root@shell ~]
[root@shell ~]
[root@shell ~]
[root@shell ~]
[root@shell ~]
3
[root@shell ~]
index1 index2 index3
[root@shell ~]
name age skill
[root@shell ~]
qiudao 18 linux
[root@shell ~]
qiudao
[root@shell ~]
18
3. 数组的遍历与循环
1. 普通数组赋值与遍历
/etc/hosts
[root@shell /scripts/shell12]
#!/bin/bash
while read line
do
hosts[i++]=$line
done < /etc/hosts
for i in ${!hosts[@]}
do
echo "hosts数组的索引为$i ,对应值为:${hosts[i]}"
done
[root@shell /scripts/shell12]
hosts数组的索引为0 ,对应值为:127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
hosts数组的索引为1 ,对应值为:::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
2. 使用关联数组统计/etc/passwd文件中的每一个shell的数量
/bin/bash
[root@shell /scripts/shell12]
30 /bin/bash
1 /bin/sync
1 /sbin/halt
22 /sbin/nologin
1 /sbin/shutdown
[root@shell /scripts/shell12]
#!/bin/bash
declare -A Shell_count
while read line
do
Type=$(echo $line|awk -F: '{print $NF}')
let Shell_count[$Type]++
done < /etc/passwd
for i in ${!Shell_count[@]}
do
echo "Shell:$i , Count:${Shell_count[$i]}"
done
[root@shell /scripts/shell12]
Shell:/sbin/nologin , Count:22
Shell:/bin/sync , Count:1
Shell:/bin/bash , Count:30
Shell:/sbin/shutdown , Count:1
Shell:/sbin/halt , Count:1
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
5
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
[root@shell /scripts/shell12]
6
3. 根据访问日志,统计出每个IP的访问次数
[root@shell /scripts/shell12]
#!/bin/bash
declare -A Ips_count
>ip.log
while read line
do
let Ips_count[${line%% *}]++
done <access.log
for i in ${!Ips_count[@]}
do
echo -e "IP:$i\tCount:${Ips_count[$i]}" >>ip.log
done
sort -t ':' -rnk3 ip.log |head
练习题
[root@shell /scripts/shell12]# cat tcp11.sh
#!/bin/bash
#1.声明关联数组
declare -A Tcp_State
#2.定义变量
Status=$(netstat -ant | awk 'NR>2{print $NF}')
#3.循环赋值
for i in $Status
do
let Tcp_State[$i]++
done
#4.索引遍历
for i in ${!Tcp_State[@]}
do
echo -e "当前Tcp状态为:$i\t\t出现的次数为:${Tcp_State[$i]}"
done
[root@shell /scripts/shell12]# sh tcp11.sh
当前Tcp状态为:ESTABLISHED 出现的次数为:2
当前Tcp状态为:LISTEN 出现的次数为:9
1. 使用while循环编写一个脚本,使数字1加到100的和
[root@shell /scripts/shell13]# cat while-1.sh
#!/bin/bash
i=1
while [ $i -le 100 ]
do
Sum=$[ $Sum + $i ]
let i++
done
echo $Sum
[root@shell /scripts/shell13]# sh while-1.sh
5050
2. 生成0-100之间的随机数,并将其相加,直到大于1000.输入相加的结果
[root@shell /scripts/shell13]# cat while-2.sh
#!/bin/bash
while true
do
Ran=$(( RANDOM % 101 ))
Sum=$(( $Sum + $Ran ))
if [ $Sum -gt 1000 ];then
echo "最后的和为:$Sum"
exit
fi
done
[root@shell /scripts/shell13]# sh while-2.sh
最后的和为:1025
3. 生成0-100之间的随机数,并将其相加,直到大于1000退出,再判断最后一个随机数是否被3整除
[root@shell /scripts/shell13]# cat while-3.sh
#!/bin/bash
while true
do
Ran=$(( RANDOM % 101 ))
Sum=$(( $Sum + $Ran ))
if [ $Sum -gt 1000 ];then
break
fi
done
Zhengshu=$(( $Ran % 3 ))
if [ $Zhengshu -eq 0 ];then
echo "最后一个随机数是可以被3整除的!这个随机数为:$Ran"
else
echo "最后一个随机数是不可以被3整除!这个随机数为:$Ran"
fi
[root@shell /scripts/shell13]# sh while-3.sh
最后一个随机数是不可以被3整除!这个随机数为:95
[root@shell /scripts/shell13]# sh while-3.sh
最后一个随机数是不可以被3整除!这个随机数为:49
[root@shell /scripts/shell13]# sh while-3.sh
最后一个随机数是可以被3整除的!这个随机数为:99