table of Contents
One, the concept of array
1. Define the array
-
Method 1:
Array name=(value0 value1 value2 …)
-
Method 2:
Array name=([0]=value [1]=value [2]=value …)
-
Method 3:
List name = "value0 value1 value2 …"
Array name = ($list name)
-
Method 4:
Array name [0] = "value"
Array name [1] = "value"
Array name [2] = "value"
2. Data types included in the array
(1)数值类型
(2)字符类型(字符串)
使用" "或''定义,防止元素当中有空格,元素按空格分割
3. Index (subscript)
- The first index is always the number 0, and the index of each subsequent element added to the array is incremented by 1
- For example, the array abc=(abcd); the index of the first data a is 0, the index of the second data b is 1, and so on.
4. Get the length of the array
arr_number=(1 2 3 4 5)
echo ${#arr_number[*]}
5. Read a subscript assignment
arr=(1 2 3 4 5)
echo ${arr[索引值]}
Two, operate on the array
1. Array traversal
#!/bin/bash
arr5=(1 2 3 4 5)
for i in ${arr5[*]}
do
echo $i
done
2. Supplement: The difference between $@ and $*
$@和$*的区别
两者没加""是什么区别
加上""
$* 将数组中的数据作为一个整体使用
$@ 将数组中每个参数分别作为单个的个体使用
$# 显示的是参数的数量,也可称为长度
Using array traversal can clearly see the difference between the two
3. Array slice
arr1=(1 2 3 4 5 6)
echo ${arr1[*]}
echo ${arr1[@]} #输出整个数组,此处*与@相同
echo ${arr1[@]:0:2} #获取 ${数组名[@或*]:起始位置:长度} 的值
4. Array replacement
格式:${数组名[*或@]/源参数值/将要替换的参数值}
arr2=(1 2 3 4 5)
#临时替换:将数组arr1中的参数值4替换为66,但原来的数组中的参数值并不会改变
echo ${arr2[*]/4/66}
echo ${arr2[*]}
#永久替换:可通过重新赋值实现
arr2=(${arr2[*]/4/66})
echo ${arr2[*]}
5. Array delete
格式:unset 数组名[索引] #删除单个
或unset 数组名 #删除整个数组
#删除单个参数值
arr3=(1 2 3 4 5)
echo ${arr3[*]}
unset arr3[2]
echo ${arr3[*]}
#删除数组
unset arr3
echo ${arr3[*]}
6. Array append
Method 1: Add a single, you need to know how many parameters are in the array, and there is no deletion of the array
arr4=(1 2 3 4 5 6 7)
echo ${arr4[*]}
arr4[7]=8 #指定索引添加参数
echo ${arr4[*]}
Method 2: Similar to method 1, when no deletion is performed, the maximum index is the length of the array minus one
arr4=(1 2 3 4 5 6 7)
echo ${arr4[*]}
arr4[${#arr4[*]}]=8 #根据长度添加参数
echo ${arr4[*]}
Method 3: Obtain all the elements of the source array directly, plus the newly added elements, and re-assign the array together and refresh the defined index to avoid deletion in the middle
arr4=(1 2 3 4 5 6 7)
echo ${arr4[*]}
arr4=("${arr4[@]}" 8 9 10) #加上新参数重新定义数组
echo ${arr4[*]}
双引号不能省略,否则数组中存在包含空格的元素时会按空格将元素拆分成多个
不能将“@”替换为“*”,如果替换为“* ”,不加双引号时与“@”的表现一致,加双引号时,会将数组array_name中的所有元素作为一个元素添加到数组中
Method 4: The elements to be added must be surrounded by "()", and multiple elements must be separated by spaces
arr4=(1 2 3 4 5 6 7)
echo ${arr4[*]}
arr4+=(10 11 12) #添加参数,这种方法用的比较多
echo ${arr4[*]}
7. Pass the array parameters to the function
(1) If an array variable is used as a function parameter, the function will only take the first value of the array variable
#!/bin/bash
test1 () {
echo "接受到的参数列表:$@"
newarrary=$1
echo "新数组的值为:${newarrary[*]}"
}
array=(3 2 1 4 5)
echo "原始数组的值为:${array[*]}"
test1 $array #将数组变量作为函数的参数,只会取数组变量的第一个值
(2) To solve this problem, you need to decompose the values of the array variables into individual values, and then use these values as function parameters. Inside the function, regroup all the parameters into a new array variable
test2() {
newarrary=($(echo $@))
echo "新数组的值为:${newarrary[*]}"
}
array=(3 2 1 4 5)
echo "原始数组的值为:${array[*]}"
test2 ${array[*]}
Example:
#!/bin/bash
qiuhe () {
arr1=(`echo $@`)
sum=0
for i in ${arr1[*]}
do
sum=$[$sum + $i]
done
echo "数组内相加结果为:$sum"
}
chengfa () {
arr2=(`echo $@`)
for ((i=0;i<=$[$# - 1];i++))
do
arr2[$i]=$[${arr2[$i]} * 2]
done
echo "乘法运算后的数组为:${arr2[*]}"
}
arr=(3 2 1 4 5)
test2 ${arr[*]}
test3 ${arr[*]}
Three, array sorting algorithm
1. Bubble sort
-
Similar to the upsurge of bubbles, the data will continue to move forward in the array from small to large or from large to small.
-
Basic idea:
The basic idea of bubble sorting is to compare the values of two adjacent elements, exchange the element values if the conditions are met, move the smaller element to the front of the array, and move the larger element to the back of the array (that is, exchange two The position of each element), so that the smaller element rises from the bottom to the top like a bubble. -
Algorithm idea: The
bubbling algorithm is implemented by a double-layer loop, where the outer loop is used to control the number of sorting rounds. Generally, the length of the array to be sorted is reduced by one, because there is only one array element left in the last loop, and there is no need for comparison. The sorting has been completed. The inner loop is mainly used to compare the size of each adjacent element in the array to determine whether to exchange positions. The number of comparisons and exchanges decreases with the number of sorting rounds.
#!/bin/bash
array=(60 20 30 50 10 40)
echo "原数组参数顺序为:${array[*]}"
for ((i=1;i<${#array[*]};i++))
do
for ((a=0;a<${#array[*]}-i;a++))
do
if [ ${array[$a]} -gt ${array[$a+1]} ]
then
temp=${array[$a]}
array[$a]=${array[$a+1]}
array[$a+1]=$temp
fi
done
done
echo "经过冒泡排序后,数组顺序为:${array[*]}"
2. Directly select sort
-
Compared with bubble sort, direct selection sort has fewer exchanges, so it is faster.
-
The basic idea:
compare the specified sort position with other array elements separately. If the condition is met, the element value will be exchanged. Note that the bubble sort is distinguished here. Instead of exchanging adjacent elements, the element that meets the condition is exchanged with the specified sort position (such as Sort from the last element), so that the sorted position gradually expands, and finally the entire array becomes the sorted format.
#!/bin/bash
array=(60 20 30 50 10 40)
echo "原数组元素顺序为:${array[*]}"
long=${#array[*]}
for ((i=1;i<$long;i++))
do
index=0
for ((a=1;a<=$long-$i;a++))
do
if [ ${array[$a]} -gt ${array[$index]} ]
then
index=$a
fi
last=$[$long-$i]
temp=${array[$last]}
array[$last]=${array[$index]}
array[$index]=$temp
done
done
echo "经过直接排序后数组元素顺序为:${array[*]}"
3. Reverse the sort
-
Reorder the contents of the original array in reverse order
-
The basic idea:
replace the last element of the array with the first element. The penultimate element is replaced with the second element, and so on, until all the array elements are reversed and replaced
#!/bin/bash
array=(60 20 30 50 10 40)
echo "反转前顺序:${array[*]}"
length=${#array[*]}
for ((i=0;i<$length/2;i++))
do
temp=${array[$i]}
array[$i]=${array[$length-1-$i]}
array[$length-1-$i]=$temp
done
echo "反转排序后:${array[*]}"