当前位置:网站首页>Shell script - array definition and getting array elements
Shell script - array definition and getting array elements
2022-07-01 08:47:00 【Little snail's way】
Definition of array
Parenthesis ( ) To represent an array , Array elements are separated by spaces
nums=(v1 v2 v3 v4 ... vn)
Be careful , Assignment number = There must be no spaces on either side , It must be next to the array name and the array elements .
Besides , You don't have to assign values to arrays one by one , The following code just assigns values to specific elements :
ages=([3]=24 [5]=19 [10]=12)
The above code is only for 3、5、10 Element assignment , So the array length is 3.
Get array elements
The following format is generally used :
${array_name[index]}
n=${nums[2]} # Said to get nums The second element of the array , And then assign it to the variable n.
echo ${nums[3]} # Indicative output nums The first of an array of 3 Elements .
Use @ or * You can get all the elements in the array , for example :
${nums[*]}
${nums[@]}
Both can get nums All elements of the array .
The code is as follows
#!/bin/bash
nums=(29 100 13 8 91 44)
echo ${nums[@]} # Output all array elements
nums[10]=66 # Give it to 10 Element assignment ( This will increase the array length )
echo ${nums[*]} # Output all array elements
echo ${nums[4]} # Output No 4 Elements
Output :
29 100 13 8 91 44
29 100 13 8 91 44 66
91
Get array length
utilize @ or *, You can extend arrays to lists , And then use # To get the number of array elements
${#array_name[@]}
${#array_name[*]}
The code is as follows :
#!/bin/bash
nums=(29 100 13)
echo ${#nums[*]}
# Adding elements to an array
nums[10]="http://c.biancheng.net/shell/"
echo ${#nums[@]}
echo ${#nums[10]}
# Delete array elements
unset nums[1]
echo ${#nums[*]}
Output :
3
4
29
3
边栏推荐
- 中小企业固定资产管理办法哪种好?
- Nacos - 配置管理
- C语言指针的进阶(下)
- 《单片机原理及应用》-片外拓展
- Embedded Engineer Interview Question 3 Hardware
- 用C语言编程:用公式计算:e≈1+1/1!+1/2! …+1/n!,精度为10-6
- Shell脚本-数组定义以及获取数组元素
- Shell脚本-if else语句
- V79.01 Hongmeng kernel source code analysis (user mode locking) | how to use the fast lock futex (Part 1) | hundreds of blogs analyze the openharmony source code
- 嵌入式工程师面试-常问问题集
猜你喜欢

《单片机原理与应用》——并行IO口原理

Screenshot tips

Installing Oracle EE

Glitch Free时钟切换技术

C语言指针的进阶(下)

Do you know how data is stored? (C integer and floating point)

What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR

Dynamic proxy

19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition

Share 7 books I read in the first half of 2022
随机推荐
中小企业固定资产管理办法哪种好?
TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
Embedded Engineer Interview frequently asked questions
MD文档中插入数学公式,Typora中插入数学公式
1.jetson与摄像头的对接
How can enterprises and developers take the lead in the outbreak of cloud native landing?
vscode自定义各个区域的颜色
The era of low threshold programmers is gone forever behind the sharp increase in the number of school recruitment for Internet companies
一文纵览主流 NFT 市场平台版税、服务费设计
Audio-AudioRecord create(一)
MATLAB【函数求导】
View drawing process analysis
MATLAB【函数和图像】
电脑小技巧
What is 1cr0.5mo (H) material? 1cr0.5mo (H) tensile yield strength
Only in China! Alicloud container service enters the Forrester leader quadrant
性能提升2-3倍!百度智能云第二代昆仑芯服务器上线
Foundation: 2 The essence of image
如何解决固定资产管理和盘点的难题?
[MFC development (16)] tree control