shell (thirteen) shell array

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/wzj_110/article/details/100541819

A Shell array

Concept : the array is a variety of types of data elements in a certain order of arrangement of the collection! Array consisting of various variables called component arrays, also known as array elements, sometimes referred to as the index variable.

An array is a special variable !

The array is a finite number of elements of data or a variable name, then numbered to distinguish between them a set of variables. The name is called array name , number called array subscript

Keywords : array name; array subscript

Classification of the array : Bash support one-dimensional array index variables and key array variables

Some explanations :

(1) Any variable used as an array index;

(2) an array of internal commands declear can explicitly declare

When unrestricted number of elements (3) of the array, not limiting array index or assignment to be continuously

(4) using an array subscript integer or arithmetic expression to access element

(5) the zero-based, and the key array using an arbitrary string to access element

Define two arrays

# 数组的一种定义形式

array=(1 2 3 4)

# 取数组的长度-->注意写法

echo ${#array[@]}

echo ${#array[*]}

# 打印数组-->数组下标从0开始

# 打印数组元素用${数组名[下标]}

echo ${array[0]}

# 打印所有的数组

echo ${array[*]} 

echo ${array[@]} 

With the same name references a number of variables, and with the number (index) to identify them!

Requirement 1 : to arbitrarily assign values to variables automatically creates an array index

# 数组名[下标]=值

array[0]=1

# 说明:数组赋值的形式增加变量

2 demand : explicitly declare an array

declare -a 数组名  # a-->array

Demand 3 : Define an array of printing and circulation

#!/bin/bash
#(1)定义数组-->大于一两个的变量的时候
array=(
172.25.2.1
172.25.2.2
172.25.2.3
)
#(2)循环打印数组
for ip in ${array[@]}
do
echo $ip
done

:<<EOF
for((i=0;i<${#array[@]};i++))
do
  echo ${array[i]}
done 
EOF

Demand 4 : Add and delete arrays

array=(1 2 3) 
# 添加
array[3]=4 
# 修改
array[1]=10
# 删除
unset array[0]

5 demand : the results of the command into an array, then print

# 体会这种用法

array=($(ls))

echo ${array[*]}

# 把系统命令结果作为数组元素

Demand 6 : intercepting and replacing the contents of the array

# 截取
array=(1 2 3 4 5 6)
# 截取1号到3号数组元素-->类似变量的截取
echo ${array[@]:1:3}
    2 3 4
echo ${array[*]:3:2}
    4 5 

# 替换
# 需求:把数组中的5替换成88,临时生效,原数组不变
echo ${array[@]/5/88}
    1 2 3 4 88 6 

Demand 7 : the internal read command -a option to the standard input an assigned group of words read arrays can also be used after it reads the value from the standard input elements of the array assigned to the specified

read -a array

Three summary

(1) the definition of

静态数组:array=(1 2 3)

动态数组:array=($(ls))

数组赋值:array[3]=4

(2) the definition of

打印所有元素:${array[@]}或${array[*]}

打印数组长度:${#array[@]}或${array[*]}

打印单个元素:${array[i]},i是数组下标

 

Guess you like

Origin blog.csdn.net/wzj_110/article/details/100541819