当前位置:网站首页>八、数组的定义
八、数组的定义
2022-07-26 22:44:00 【JXin-xxx】
八、数组的定义
数组是存放相同类型数据的集合,在内存中开辟了连续的空间,通常配合循环使用
数组的分类
普通数组:不需要声明直接定义,下标索引只能是整数
关联数组:需要用declare -A声明否则系统不识别,索引可以是字符串
数组的定义方式
| ( | 11 | 22 | 33 | 44 | 55 | 66 | ) |
|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | 5 |
第一种:直接把要加入数组的元素用小括号括起来,中间用空格分开
num=(11 22 33 44)
${#num}显示字符串长度
数组名=(value0 value1 value2)
第二种:精确的给每一个下标索引定义一个值加入数组,索引数字可以不连续
num=([0]=55 [1]=66 [2]=77 [4]=88)
数组名=([0]=value [1] =value [2] =value...)
第三种:先把要加入数组的元素全部先赋值给一个变量,然后应用这个变量加入到数组
list=“11 12 13 14” 列表名=“value0 value1 value2...”
num=($list) 数组名=($列表名)
第四种:根据下标定义
数组名[0]="11" 数组名[0]="value"
数组名[1]="22" 数组名[1]="value"
数组名[2]="33" 数组名[2]="value"
数组包括的数据类型
数值类型
字符类型:使用 “ ” 或 ‘ ’ 定义
获取数组的长度
arr_number=(10 20 30 40 50)
arr_length=${#arr_number[*]}
或: arr_length=${#arr_number[@]}
echo $arr_length
echo ${arr_number[*]}
或 echo ${arr_number[@]}
数组元素遍历
[[email protected] ~]# vim ysszbl.sh
[[email protected] ~]# sh ysszbl.sh
1
2
3
4
5
6
[[email protected] ~]# cat ysszbl.sh
#!/bin/bash
arr=(1 2 3 4 5 6)
for i in ${arr[*]} # for i in ${arr[@]}
do
echo $i
done
元素切片
[[email protected] ~]# arr=(1 2 3 4 5 6 7 8)
[[email protected] ~]# echo ${arr[@]} #输出整个数组
1 2 3 4 5 6 7 8
[[email protected] ~]# echo ${arr[*]:2:3} #提取从索引下标2开始的3个元素
3 4 5
[[email protected] ~]# echo ${arr[*]:0:4} #提取从下标0开始的4个元素
1 2 3 4
[[email protected] ~]#
数组(元素)替换
[[email protected] ~]# arr=(11 12 13 14 15 16)
[[email protected] ~]# echo ${arr[*]/3/44} #${数组名【@或*】/查找字符/替换字符}
11 12 144 14 15 16
[[email protected] ~]# echo ${arr[*]/1/32}
321 322 323 324 325 326
[[email protected] ~]# echo ${arr[*]} #不会替换数组原有的内容
11 12 13 14 15 16
[[email protected] ~]# arr=(${arr[*]/1/32}) #要实现改变原有数组,可通过重新赋值实现
[[email protected] ~]# echo ${arr[*]}
321 322 323 324 325 326
数组删除
[[email protected] ~]# arr=(1 2 3 4 5)
[[email protected] ~]# unset arr #删除数组
[[email protected] ~]# echo ${arr[*]}
[[email protected] ~]# arr=(1 2 3 4 5)
[[email protected] ~]# unset arr[4] #删除下标为4的元素
[[email protected] ~]# echo ${arr[*]}
1 2 3 4
[[email protected] ~]#
小结:数组
1、数组定义方式
num=(11 12 13 14 11 15)
以上表示定义一个集合(定义一个数组的所有元素)
可以使用以下命令来查看数组每个元素的值和数组的长度
2、查看数组的长度
echo ${num[@]}
echo ${num[*]}
3、查看数组的长度
echo ${#num[@]}
echo ${#num[*]}
查看元素(echo) ${数组名[下标]}
"@“或”*"表示所有
4、元素赋值
arr[0]=“12”
数组名[下标]=元素值
5、数组切片
echo ${arr[*]:2:1}
echo ${数组名[*]:下标:打印几个元素值}
echo ${数组名[@]:下标:打印几个元素值}
6、数组(元素)替换-----临时
echo ${arr[*]/3/55}
echo ${数组名[*]/字符(串)/替换为指定值(仅替换每个元素的第一个匹配项)}
echo ${数组名[@]/字符(串)/替换为指定值(仅替换每个元素的第一个匹配项)}
7、数组删除
10 20 30 40 50
0 1 2 3 4
unset arr[2] #删除下标为2的元素------------------》
10 20 40 50
0 1 2 3 4
以数组的方式随机点名
[[email protected] opt]# cat dm.sh
#!/bin/bash
list=$(cat /opt/name) #/opt/name点名的名单
arr=($list) #定义数组
for i in {arr[*]}
do
a=$[RANDOM%25] #a为下标值,0-24随机数
echo "${arr[$a]}"
done
[[email protected] opt]#
边栏推荐
- [unity] unity interface scene view [1]
- 07 - setup and attack of log server
- Navicat操作数据库
- MySQL字符集设置为UTF-8,但控制台仍然出现中文乱码问题
- Esp8266---json---c function library provides string functions
- #高级语言 各种开发软件介绍
- ESP8266---JSON数据交换格式
- Basic DOS commands
- Are you ready for the Internet of things to revolutionize manufacturing?
- 十三、命令小工具
猜你喜欢

The MySQL character set is set to UTF-8, but the console still has the problem of Chinese garbled code

Question making notes 1
![[unity] unity interface scene view [1]](/img/5a/c34ff09ef1ddba4b65c7873775c251.png)
[unity] unity interface scene view [1]

Doris or starrocks JMeter pressure measurement

Come and help you understand the Internet of things in three minutes

if 与 else if 的区别

力扣刷题量300记录帖

ESP8266连接乐鑫云平台IOT_Demo

Next generation Internet: Video Networking

Mqtt protocol ----- above
随机推荐
ESP8266 STA_UDP_Client
Unity screenshot widget
Dream journey
Come on: encourage college graduates to return home to start businesses and employment, and help rural revitalization
SQL relational algebra - Division
Esp8266---json---c function library provides string functions
Li Hongyi machine learning (2021 Edition)_ P7-9: training skills
Unity Line接入
4. Root user login
Mqtt---- bottom (precautions)
Unity CharacterController
RS485 signal measurement
Adding, deleting, checking and modifying dynamic sequence table with C language
Flinksql window triggered in advance
Code merging of centralized version control tools
Some simple extension methods commonly used by unity
以赛促练-力扣第303场周赛反思
最大公约数的求法
5. Legal bracket string
Promoting practice with competition - Reflection on the 303th weekly match