shell/shell数组.md

206 lines
4.5 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1><center>shell数组</center></h1>
------
## 一:数组
#### 1.数组介绍
是若干数据的集合,其中的每一份数据都称为元素
shell不限制数组的大小理论上可以存放无限量的数据
#### 2.数组分类
普通数组
只能使用整数作为数组索引/下标(从0开始)
关联数组
可以使用字符串作为数组索引/下标
#### 3.定义数组
##### 普通数组
方法一: 一次赋一个值
```shell
数组名[索引]=变量值
[root@xingdiancloud ~]# array1[0]=pear
[root@xingdiancloud ~]# array1[1]=apple
[root@xingdiancloud ~]# array1[2]=orange
[root@xingdiancloud ~]# array1[3]=peach
```
方法二: 一次赋多个值
```shell
[root@xingdiancloud ~]# array2=(tom jack alice)
[root@xingdiancloud ~]# array5=(tom jack alice "bash shell")
[root@xingdiancloud ~]# colors=($red $blue $green $recolor)
[root@xingdiancloud ~]# array5=(1 2 3 4 5 6 7 "linux shell" [20]=saltstack)
[root@xingdiancloud ~]# array8=`cat /etc/passwd`
```
##### 关联数组
申明该数组为关联数组
```shell
[root@xingdiancloud ~]# declare -A ass_array1
[root@xingdiancloud ~]# declare -A ass_array2
```
方法一: 一次赋一个值
```shell
数组名[索引]=变量值
[root@xingdiancloud ~]# ass_array1[index1]=pear
[root@xingdiancloud ~]# ass_array1[index2]=apple
[root@xingdiancloud ~]# ass_array1[index3]=orange
[root@xingdiancloud ~]# ass_array1[index4]=peach
```
方法二: 一次赋多个值
```shell
[root@xingdiancloud ~]# ass_array2=([index1]=tom [index2]=jack [index3]=alice [index4]='bash shell')
```
一步走
```shell
[root@xingdiancloud ~]# declare -A ass_array1='([index4]="peach" [index1]="pear" [index2]="apple" [index3]="orange" )'
[root@xingdiancloud ~]# declare -A ass_array2='([index4]="bash shell" [index1]="tom" [index2]="jack" [index3]="alice" )'
```
#### 4.访问数组元素
##### 普通数组
```shell
[root@xingdiancloud ~]# echo ${array1[0]} 访问数组中的第一个元数
[root@xingdiancloud ~]# echo ${array1[@]} 访问数组中所有元数 等同于 echo ${array1[*]}
[root@xingdiancloud ~]# echo ${#array1[@]} 统计数组元数的个数
[root@xingdiancloud ~]# echo ${!array2[@]} 获取数组元数的索引
[root@xingdiancloud ~]# echo ${array1[@]:1} 从数组下标1开始
[root@xingdiancloud ~]# echo ${array1[@]:1:2} 从数组下标1开始访问两个元素
```
##### 关联数组
```shell
[root@xingdiancloud ~]# echo ${ass_array2[index2]} 访问数组中的第二个元数
[root@xingdiancloud ~]# echo ${ass_array2[@]} 访问数组中所有元数 等同于 echo ${array1[*]}
[root@xingdiancloud ~]# echo ${#ass_array2[@]} 获得数组元数的个数
[root@xingdiancloud ~]# echo ${!ass_array2[@]} 获得数组元数的索引
```
#### 5.数组遍历
方法一: 通过数组元数的个数进行遍历
方法二: 通过数组元数的索引进行遍历
案例一:利用元素进行遍历
```shell
#!/bin/bash
#定义数组
array=(Mon Tue Wed Thur Fir Sat Sun)
#数组遍历
for day in ${array[*]}
do
echo $day
done
```
案例二:利用索引进行遍历
```
[root@xingdiancloud ~]# cat hello.sh
#!/bin/bash
for line in `cat /etc/hosts`
do
hosts[++j]=$line
done
for i in ${!hosts[@]}
do
echo "$i : ${hosts[i]}"
done
[root@xingdiancloud ~]# bash hello.sh
1 : 127.0.0.1
2 : localhost
3 : localhost.localdomain
4 : localhost4
5 : localhost4.localdomain4
6 : ::1
7 : localhost
8 : localhost.localdomain
9 : localhost6
10 : localhost6.localdomain6
```
定义换行符
```shell
[root@xingdiancloud ~]# cat hello.sh
#!/bin/bash
IFS=$'\n'
for line in `cat /etc/hosts`
do
hosts[++j]=$line
done
for i in ${!hosts[@]}
do
echo "$i : ${hosts[i]}"
done
[root@xingdiancloud ~]# bash hello.sh
1 : 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
2 : ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
```
#### 6.项目案例
通过数组统计性别:把要统计的对象作为数组的索引
```shell
[root@xingdiancloud ~]# cat sex.txt
zhangsan f
lisi m
[root@xingdiancloud ~]# cat sex.sh
#!/usr/bin/bash
declare -A sex
while read line
do
type=`echo $line | awk '{print $2}'`
let sex[$type]++
done < sex.txt
#m作为数组的索引
for i in ${!sex[@]}
do
echo "$i : ${sex[$i]}"
done
```