shell--数组、正则表达式RE
1.数组
1.1定义
什么是数组?
数组也是一种变量,常规变量只能保存一个值,数组可以保存多个值
1.2 分类
普通数组:只能用整数作为数组的索引--0 下标
有序数组(普通数组):(index)索引(为整数,从0开始)
关联数组:可以使用字符串作为数组的索引
1.3 普通数组
引用: echo ${array_name[index]} #引用
[root@linux-server script]# books=( linux shell awk sed ) ---在python中叫列表
[root@linux-server script]# echo ${books[0]}
linux
[root@linux-server script]# echo ${books[1]}
shell
[root@linux-server script]# echo ${books[2]}
awk
1.4 关联数组
需要提前声明
Declare命令:
[test@test test]# declare [-选项]
参数说明:
-a :#定义为数组--array 声明
-A : #定义关联数组
declare -A myarry1
[root@linux-server script]# declare -A myarry1
[root@linux-server script]# myarry1=([name]=soso666 [sex]=man [age]=18)
[root@linux-server script]# echo ${myarry1[name]}
soso666
[root@linux-server script]# echo ${myarry1[age]}
18
1.4 数组定义方法
定义方法1:
# array=( one two three four five six )
# array1=(`cat /etc/passwd`) #希望是将文件中的每一行作为一个值赋给数组array3
# array2=(1 2 3 4 5 6 7 "linux shell" [20]=saltstack)定义方法2:指定索引赋值
#语法:数组名[index]=变量值
[root@linux-server script]# vim shuzu.sh
#!/bin/bash
NAME[0]="BJ"
NAME[1]="SH"
NAME[2]="SZ"
NAME[3]="GZ"
NAME[4]="HZ"
NAME[5]="ZZ"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}"
echo "sixth Index: ${NAME[5]}"输出结果
[root@linux-server script]# bash shuzu.sh
First Index: BJ
Second Index: