How the array is made

Array

definition

  • Array is a collection of column numbers, we use index to distinguish it
  • The index starts from 0, 0 1 2 3 4…

Array definition method

method one

  • Array name=(value0 value1 value2 …)

Insert picture description here

Method Two

  • Array name=([0]=value [1]=value [2]=value …)

Insert picture description here

Method Three

  • List name = "value0 value1 value2 …"
  • Array name = ($list name)
    Insert picture description here
    Insert picture description here

Method Four

  • Array name [0] = "value"
  • Array name [1] = "value"
  • Array name [2] = "value"
    Insert picture description here

View the contents of the array

Use echo to view

echo ${a[*]}

Insert picture description here

Data type contained in the array

  • Numeric type
  • Character type
    Use "" or '' to define

Get array length

a=(1 2 3 4 5)
a_long=$(#a[*] ) #Assign the obtained length to a variable, so that it is more convenient to use
echo $a_long

Insert picture description here

Get a subscript assignment

arr_index2=${arr[2]}
echo $arr_index2  

Insert picture description here

Array traversal

#!/bin/bash
a=(1 2 3 4 5)
for v in $ {a [e]}
do
echo $v
done

Insert picture description here

Insert picture description here

Array slice

arr=(1 2 3 4 5)
echo ${arr[@] }  #输出整个数组
echo $ {arr[@] :0:2}   获取${数组名〔@或*]:起始位置:长度}的值
echo ${arr1[ * ] :1 :3}
echo ${arr[0]:2:3}

Insert picture description here

Array replacement

arr=(1 2 3 4 5)
echo $ {arr[@]/4/66} #$ {array name[@ or *]/find character/replace character}
echo $ {arr[@]} #do not Replace the original content of the array
arr=($ {arr[@]/4/66}) #To change the original array, you can implement
echo $ {arr[@]}
Insert picture description here

Array delete

arr=(1 2 3 4 5)
unset arr     #删除数组
echo ${arr[* ]}
arr= (1 2 3 4 5)
unset arr[2]     #删除第三个元素
echo $ {arr[* ]}

Insert picture description here
Insert picture description here

Add a new number to the array

方法一:
array _name [ index]=value
方法二:
array _name [${#array_name [@] } ]=value
方法三:
array_name=("$ {array name [ 0] } " value1 ... valueN)
双引号不能省略,否则,当数组array name中存在包含空格的元素时会按空格将元素拆分成多个
不能将"@"替换为"*",如果替换为",不加双引号时与"e"的表现一致,加双引号时,会将数组array_name中的所有元素作为一个元素加到数组中
方法四:
array name+= (value1 ... valueN)
待添加元素必须用"()“包围起来,并且多个元素用空格分隔

Personally, I suggest using the fourth one, which is convenient and fast, with few bugs.

Insert picture description here

Pass array parameters to the function

test() {
   echo "接收到的参数列表:$@"
   newa=$1
   echo "新数组的值为:${newa[*]}"
   }
   
a=(3 2 1 4 5)
echo "原始数组的值为:${a[*]}"
test1 $a

Insert picture description here
Insert picture description here

  • We found that if we use a variable as a function parameter, the function will only take the first value of the array variable. At this time, if we need to solve this problem, we need to decompose the values ​​of the array into individual values, and then use these values ​​as the array variables of the function.

Insert picture description here

Return array from function

test2(){
    newa=(`echo $@`)
 sum=0
 for value in ${newa[*]}
 do
    sum=$[$sum + $value]
 done
 echo $sum
}

test3() {
   newa=(`echo $@`)
   for ((i=0;i<=$[$# - 1];i++))
   {
     newa[$i]=$[${newa[$i]} * 2]
   }
   echo ${newa[*]}
   }


a=(3 2 1 4 5)
echo "原始数组的值为:${a[*]}"
resultl=`test2 ${a[*]}`
echo "原始数组的和为:$resultl"
result2=(`test3 ${a[*]}`)
echo "新数组的值为:${result2[*]}"

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51614581/article/details/111571466