[Advanced Operation and Maintenance Knowledge] Super detailed explanation of Shell programming 5 (ordinary array + associative array + lottery project)

This article continues to introduce you to Shell programming, and introduces you to array-related content. With the previous content, you can basically complete most of the requirements in Shell.

Table of contents

array

1. Ordinary array

1. Grammatical structure

2. Array definition method

3. Ping the following IPs to check whether they can be pinged (the value defined by the array is taken out by a for loop)

2. Associative arrays

1. Determine the number of men and women in the text

2. Count the number of occurrences of IP

lottery project


array

The previous variable is like a box containing one thing, and an array is equivalent to one box containing multiple small boxes, and the small boxes contain things, and each thing has a number

1. Ordinary array

Only numeric integers can be used as indexes (element names, also called subscripts)

1. Grammatical structure

数组名称[元素名称]=元素的值

2. Array definition method

1. According to the index definition

[root@Web01 ~]# array[0]=shell
[root@Web01 ~]# array[1]=mysql
[root@Web01 ~]# array[2]=docker

View the value of the array

[root@Web01 ~]# echo ${array[0]}
shell
[root@Web01 ~]# echo ${array[1]}
mysql
[root@Web01 ~]# echo ${array[2]}
docker
[root@Web01 ~]# echo ${array[*]}
shell mysql docker
[root@Web01 ~]# echo ${array[@]}
shell mysql docker

View array index (subscript, element name)

[root@Web01 ~]# echo ${!array[*]}
0 1 2

cancel array

[root@Web01 ~]# unset array
[root@Web01 ~]# echo ${!array[*]}

[root@Web01 ~]#

2. Direct assignment definition

Note that the default index starts from 0

[root@Web01 ~]# array=(shell test hehe docker k8s)
[root@Web01 ~]# echo ${!array[*]}
0 1 2 3 4
[root@Web01 ~]# echo ${array[*]}
shell test hehe docker k8s

3. Hybrid definition

[root@Web01 ~]# array=([5]=shell test hehe docker [20]=k8s)
[root@Web01 ~]# echo ${!array[*]}
5 6 7 8 20
[root@Web01 ~]# echo ${array[*]}
shell test hehe docker k8s

4. Support command value definition

[root@Web01 ~]# array=(`echo shell mysql redis`)
[root@Web01 ~]# echo ${array[*]}
shell mysql redis
[root@Web01 ~]# echo ${!array[*]}
0 1 2

3. Ping the following IPs to check whether they can be pinged (the value defined by the array is taken out by a for loop)

10.0.0.2
10.0.0.4
202.106.0.20
114.114.114
223.5.5.5
8.8.8.8

It can be traversed by taking the array value 

[root@LB00 Day05]# cat ping.sh
#!/bin/basha
ip=(
10.0.0.2
10.0.0.4
202.106.0.20
114.114.114
223.5.5.5
8.8.8.8
1.5.4.9
)
for i in ${ip[*]}
do
	ping -c1 -W1 $i &> /dev/null
	if [ $? == 0 ];then
		echo $i 可以通 
	else
		echo $i 不通
	fi
done
[root@LB00 Day05]# sh ping.sh
10.0.0.2 可以通
10.0.0.4 可以通
202.106.0.20 不通
114.114.114 不通
223.5.5.5 可以通
8.8.8.8 可以通
1.5.4.9 不通

You can also traverse by indexing

[root@LB00 Day05]# cat ping.sh
#!/bin/basha
ip=(
10.0.0.2
10.0.0.4
202.106.0.20
114.114.114
223.5.5.5
8.8.8.8
1.5.4.9
)
for i in ${!ip[*]}
do
       ping -c1 -W1 ${ip[$i]} &> /dev/null
       if [ $? == 0 ];then
               echo ${ip[$i]} 可以通 
       else
               echo ${ip[$i]} 不通
       fi
done
[root@LB00 Day05]# sh ping.sh
10.0.0.2 可以通
10.0.0.4 可以通
202.106.0.20 不通
114.114.114 不通
223.5.5.5 可以通
8.8.8.8 可以通
1.5.4.9 不通

2. Associative arrays

can be indexed by a string

The definition method is the same as that of an ordinary array, and the string array does not recognize it, so it will default to 0

[root@LB00 Day05]# array[index1]=shell
[root@LB00 Day05]# array[index2]=mysql
[root@LB00 Day05]# array[index3]=redis
[root@LB00 Day05]# echo ${array[*]}
redis
[root@LB00 Day05]# echo ${!array[*]}
0

Declared as an associative array, let the string recognize

[root@LB00 Day05]# unset array
[root@LB00 Day05]# declare -A array
[root@LB00 Day05]# array[index1]=shell
[root@LB00 Day05]# array[index2]=mysql
[root@LB00 Day05]# array[index3]=redis
[root@LB00 Day05]# echo ${array[*]}
shell mysql redis
[root@LB00 Day05]# echo ${!array[*]}
index1 index2 index

1. Determine the number of men and women in the text

[root@LB00 Day05]# cat xingbie.txt
boy
girl
boy
boy
girl
xxx
[root@LB00 Day05]# declare -A array
[root@LB00 Day05]# let array[boy]++    #通过数组元素值自增去判断数量
[root@LB00 Day05]# echo ${array[boy]}    #就是元素值是数字,下标是文字
1
[root@LB00 Day05]# let array[boy]++
[root@LB00 Day05]# echo ${array[boy]}
2

[root@LB00 Day05]# cat array.sh
#!/bin/bash
declare -A array
while read line
do
	let array[$line]++
done<xingbie.txt

for i in ${!array[*]}
do
	echo $i 出现了 ${array[$i]} 次
done
[root@LB00 Day05]# sh array.sh
xxx 出现了 1 次
girl 出现了 2 次
boy 出现了 3 次

2. Count the number of occurrences of IP

[root@LB00 Day05]# cat count_ip.sh
#!/bin/bash
declare -A array
while read line
do
	ip=`echo $line|awk '{print $1}'`
	let array[$ip]++
done</var/log/nginx/access.log

for i in ${!array[*]}
do
	echo $i 出现了 ${array[$i]} 次
done
[root@LB00 Day05]# sh count_ip.sh
172.16.1.5 出现了 207 次

lottery project

need:

1. Enter the name, and a random number from 1-100 will appear after the name

2. The numbers that have appeared can no longer appear

3. The TOP ranking of the last random number

[root@LB00 Day05]# cat zhuajiu.sh
#!/bin/bash
while true
do
menu(){
echo -e "\t\t\t\033[31m 1.抓阄 \033[0m"
echo -e "\t\t\t\033[32m 2.当前排名 \033[0m"
}
menu
read -p "请输入模式的编号[1|2]: " moshi
case $moshi in
	1)
	read -p "请输入你的姓名: " name
	while true
	do
	ran=`echo $((RANDOM%100+1))`
	if [ -f ran.txt ];then
		if [ `grep -w $ran ran.txt|wc -l` == 1 ];then
			continue
		else
			echo $name $ran >> ran.txt 
			break
		fi
	else
		echo $name $ran >> ran.txt
		break
	fi
	done
	echo 名字为 $name 号码为 $ran
	;;
	2)
	echo 抓阄排行如下
	if [ -f ran.txt ];then
		sort -rnk2 ran.txt
	else
		echo 当前还没有排名
	fi
	;;
	*)
	echo "Usage: [1|2]"
esac
done
[root@LB00 Day05]# sh zhuajiu.sh
			 1.抓阄 
			 2.当前排名 
请输入模式的编号[1|2]: 1
请输入你的姓名: q
名字为 q 号码为 32
			 1.抓阄 
			 2.当前排名 
请输入模式的编号[1|2]: 1
请输入你的姓名: w
名字为 w 号码为 55
			 1.抓阄 
			 2.当前排名 
请输入模式的编号[1|2]: 1
请输入你的姓名: e
名字为 e 号码为 51
			 1.抓阄 
			 2.当前排名 
请输入模式的编号[1|2]: 2
抓阄排行如下
w 55
e 51
q 32
			 1.抓阄 
			 2.当前排名 
请输入模式的编号[1|2]: ^Z
[17]+  Stopped                 sh zhuajiu.sh

I am koten, with 10 years of experience in operation and maintenance, and I continue to share dry goods in operation and maintenance. Thank you for your reading and attention!

 

Guess you like

Origin blog.csdn.net/qq_37510195/article/details/130739023