Array of basic shell programming teaching

One, the method of defining an array

1.1 Method One

Array name=(value 1 value 2 value 3... …value n)
example:
Insert picture description here
A value in an array is an element
Method 1 From the first element to the last element will default to an index (or called a subscript)
Start from 0, that is, the first element defaults to 0, the second element defaults to 1... …and so on
as the picture shows:

Insert picture description here

1.2 Method two

Array name=([0]=value 1 [1]=value 2 [2]=value 3... …)
Example:
Insert picture description here
Method two is equivalent to assigning an index to the value. The advantage of this method is that you can skip an index, and you can use the skipped index to insert the value later.

1.3 Method Three

List name = "Number 1 Number 2 Number 3... …
Number n" Array name = ($List name)

Example:
Insert picture description here

1.4 Method 4

Array name [index number] = "number 1"
array name [index number] = "number 2"
array name [index number] = "number 3"

example:
Insert picture description here
The index skipped in method two can be re-assigned with this method

Two, the data types included in the array

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

Three, get the length of the array

Array length ${Array name[*]}
Array length${Array name[@]}

Insert picture description here

Four, read a certain index (subscript) assignment

echo ${array name[index]}
Insert picture description here

Five, array traversal

:
Insert picture description here

Six, array slicing

echo ${array name[* or @]: index starting position: number of indexes}
Insert picture description here

Seven, array replacement

Method 1:
echo ${array name[* or @]/search character/replace character}
Insert picture description here

Note: Method one is temporary replacement, if you want to replace permanently, you can use method two to re-assign to achieve
Method 2:
Array name=(${array name[* or @]/search character/replace character})
Insert picture description here

8. Value deletion

8.1 Array deletion

unset array name
Insert picture description here

8.2 Character deletion

unset array name [index]
Insert picture description here
Note: The index corresponding to the deleted array is still there, but there is no corresponding value

Nine, add elements to the array

Method one:
array name [index]=element
Insert picture description here
Method two:
array name [${#array name[* or @]}]=element
Insert picture description here

此方法再添加之前数组要是连续的,不能有空着的序列

Method three:

Array name=("${Array name[@]}" element 1 element 2... …)
Insert picture description here
Insert picture description here

双引号不能省略,否则,当数组中存在包含空格的元素时会按空格将元素拆分成多个
不能将"@"替换为"*",如果替换为"*",不加双引号时与"@"的表现一致,加双引号时,会将数组中的所有元素作为一个元素添加到数组中

Method four:
array name += (element 1 element 2 element 3...)
Insert picture description here

10. Pass array parameters to the function

test1() {
newshuzu=($(echo $@))
   echo "新数组的值为:${newshuzu[*]}"

}

shuzu=(2 3 5 1 6)
echo "原始数组的值为:${shuzu[*]}"
test1 ${array[*]}

Insert picture description here
Insert picture description here

11. Return an array from a function

test2() {
newshuzu=(`echo $@`)                   #将数组传入函数,$@表示将每个元素分开输出"3" "2" "1" "4" "5"
sum1=0 
for sum2 in ${newshuzu[*]}             #for循环来求新数组newshuzu的和,,in可以以空格分隔数组中的元素进行提取
do
sum1=$[$sum1 + $sum2]                  #求元素和
done
echo $sum1                             #输出元素之和(即result1)
}
test3 () { 
newshuzu=(`echo $@`)                   #将数组传入函数,$@表示将每个元素分开输出"3" "2" "1" "4" "5"
for ((i=0;i<=$[$# - 1];i++))           #$#表示新数组元素的总个数,因为这边的i同时也表示数组的索引号,所以要减1,即[0][1][2][3][4]   
 do    
newshuzu[$i]=$[${newshuzu[$i]}*2]      #表示将新数组的每个元素 *2形成新的元素值
 done
echo ${newshuzu[*]}                    #输出 *2后的数组,即result2
}
shuzu=(3 2 1 4 5)
echo "原始数组的值为: ${shuzu[*]}"
result1=`test2 ${shuzu[*]}`            #调用test2函数
echo "新数组的和为: $result1"
result2=`test3 ${shuzu[*]}`            #调用tset3函数
echo "新数组的值为: $result2"

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51613313/article/details/111590055