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:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值