欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > shell--数组、正则表达式RE

shell--数组、正则表达式RE

2025/4/29 10:53:35 来源:https://blog.csdn.net/m0_74201628/article/details/147595508  浏览:    关键词: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:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词