Introduction to arrays
In bash script programming, a variable is a memory space for storing a single element; an array is a continuous memory space for storing multiple elements.
The array is composed of array name and subscript, as follows.
ARRAY_NAME[SUBSCRIPT]
Arrays can be divided into two types according to the type of subscript:
- Indexed array: subscripts are non-negative integers such as 0, 1, 2, etc.
- Associative (associative) array: The subscript is a user-defined string.
Array operations
statement
The index array can be used directly without declaration; and if the associative array is used directly without declaration, it will be regarded as an index array, even if its subscript is a string.
The way the index array is declared.
# declare -a ARRAY_NAME
The way the associative array is declared.
# declare -A ARRAY_NAME
Assignment
Assign only one element at a time.
# ARRAY_NAME[SUBSCRIPT]=VALUE
Assign all elements at once.
# ARRAY_NAME = ("VAL1" "VAL2" "VAL3" ...)
Assigning multiple elements at once can be discontinuous.
# ARRAY_NAME = ([0] = "VAL1" [3] = "VAL3")
Such an array that does not require elements to exist in sequence (that is, A[3] can be assigned when there is no A[1] and A[2]), it is called a sparse format array. Therefore, bash supports arrays in sparse format.
Read the standard input assignment array.
# read -a ARRAY_NAME
When inputting, use a space as the separator of the element, and end the assignment of the element with the enter key.
Append elements to the end of the array.
ARRAY_NAME[${#ARRAY_NAME[@]}]=VALUE
or
ARRAY_NAME+=(VALUE)
Quote
Refer to a single array element.
${ARRAY_NAME[SUBSCRIPT]}
If SUBSCRIPT is omitted, it is equivalent to SUBSCRIPT=0. That is, the following two references are the same.
${ARRAY_NAME[0]} ${ARRAY_NAME}
The subscript can be a variable. When the subscript is a variable, the expansion subscript variable character "$" can be omitted. That is, ${array[$var]}=${array[var]}.
[root@c7-server ~]# declare -a name= (bu jing yun) [root@c7-server ~]# echo ${name[*]} bu jing yun [root@c7-server ~]# declare -i a=0 b=1 c=2 [root@c7-server ~]# echo ${name[$a]} this [root@c7-server ~]# echo ${name[$b]} jing [root@c7-server ~]# echo ${name[$c]} yun [root@c7-server ~]# echo ${name[a]} this [root@c7-server ~]# echo ${name[b]} jing [root@c7-server ~]# echo ${name[c]} yun
Reference all elements of the array. Under normal circumstances, there is no difference between the two, only when wrapped in double quotes, "@" is expanded to each element as a separate word; "*" is expanded to all elements as a unified word.
${ARRAY_NAME[@]}
${ARRAY_NAME[*]}
Refers to the length of the array element.
${#ARRAY_NAME[SUBSCRIPT]}
The length of the reference array, that is, the number of elements in the array.
${#ARRAY_NAME[@]}
${#ARRAY_NAME[*]}
Reference to some elements (slices) of an array.
${ARRAY_NAME[@]:OFFSET:NUMBER}
${ARRAY_NAME[*]:OFFSET:NUMBER}
OFFSET: Offset, which means to offset/skip the first few elements in the array.
NUMBER: Indicates how many elements are taken after the offset.
If NUMBER is omitted, and the value of OFFSET is "-n" (note that there is a space to the left of -n), it means that the n elements from the bottom are quoted.
So far we are referencing the value of the array. If we want to refer to the subscript of the array, we can use:
${!ARRAY_NAME[@]}
${
!ARRAY_NAME[*]}
delete
Delete array elements.
# unset ARRAY_NAME[SUBSCRIPT]
Delete the array.
# unset ARRAY_NAME
Array example
Define an index array and assign array elements one by one.
[root@c7-server ~]# declare -a my_array [root@c7-server ~]# my_array[0]=zhang [root@c7-server ~]# my_array[1]=wen [root@c7-server ~]# my_array[2]=long
Get the array elements according to the array index. Pay attention to what we said above, when referencing an array without a subscript, it is equivalent to referencing ${ARRAY_NAME[0]}.
[root@c7-server ~]# echo ${my_array} zhang [root@c7-server ~]# echo ${my_array[0]} zhang [root@c7-server ~]# echo ${my_array[1]} wen [root@c7-server ~]# echo ${my_array[2]}
long
Quote all the elements in the array and test the difference between "@" and "*" by the way. Note that this difference will only appear when ${my_array[@]} or ${my_array[*]} is wrapped in double quotes.
[root@c7-server ~]# echo ${my_array[@]} zhang wen long [root@c7-server ~]# echo ${my_array[*]} zhang wen long [root@c7-server ~]# for i in "${my_array[@]}"; do echo $i; done zhang wen long [root@c7-server ~]# for i in "${my_array[*]}"; do echo $i; done zhang wen long
The number of reference arrays.
[root@c7-server ~]# echo ${#my_array[@]} 3 [root@c7-server ~]# echo ${#my_array[*]} 3
Refers to the number of elements in the array.
[root@c7-server ~]# echo ${my_array[0]} zhang [root@c7-server ~]# echo ${#my_array[0]} 5
Next, we will demonstrate several other different assignment methods. You can delete the array before operating.
[root@c7-server ~]# unset my_array [root@c7-server ~]# my_array=([0]=zhang [1]=wen [2]=long) [root@c7-server ~]# echo ${my_array[@]} zhang wen long [root@c7-server ~]# unset my_array [root@c7-server ~]# read -a my_array Mon Tue Wed Thu Fri Sat Sun [root@c7-server ~]# echo ${my_array[@]} Mon Tue Wed Thu Fri Sat Sun
Array elements to substring (substring), that is, slices.
[root@c7-server ~]# echo ${my_array[@]} Mon Tue Wed Thu Fri Sat Sun [root@c7-server ~]# echo ${my_array[@]:3:2} Thu Fri [root@c7-server ~]# echo ${my_array[@]:2:3} Wed Thu Fri [root@c7-server ~]# echo ${my_array[@]: -3} Fri Sat Sun
Array elements are appended.
[root@c7-server ~]# echo ${my_array[@]} Mon Tue Wed Thu Fri Sat Sun [root@c7-server ~]# my_array+=(ddd) [root@c7-server ~]# my_array[${#my_array[@]}]=eee [root@c7-server ~]# echo ${my_array[@]} Mon Tue Wed Thu Fri Sat Sun ddd eee
Refer to the subscript of the array. I personally feel that reference array subscripts are more useful in associative arrays, but not very useful in index arrays.
[root@c7-server ~]# echo ${!my_array[@]} 0 1 2 3 4 5 6 7 8 [root@c7-server ~]# unset my_array [root@c7-server ~]# declare -A my_array [root@c7-server ~]# my_array=([name]=zwl [age]=28 [sex]=male) [root@c7-server ~]# echo ${my_array[@]} zwl 28 male [root@c7-server ~]# echo ${!my_array[@]} name age sex
Practice questions
Question 1: Generate 10 non-negative integers within 100 and store them in the array, sort them, and output the maximum and minimum values.
This question involves bubble sorting in the algorithm. You can refer to the concept explanation in " Bubble Sort_ Baidu Encyclopedia " and the icon in "1.1 Bubble Sort | Novice Tutorial" . This GIF picture is really super Baton!
After sorting in ascending order, the maximum and minimum values can be selected from the beginning and the end.
Code example.
#!/bin/bash declare -a rand declare -i rand_length var for i in { 0..9}; do rand[$i]=$((RANDOM%100)) done echo -e "The original array is \t${rand[*]}." rand_length=${#rand[*]} #echo $rand_length for i in $(seq 0 $((rand_length-1)) | sort -nr); do for((j=0;j<$i;j++)); do if [ ${rand[j]} -gt ${rand[((j+1))]} ]; then var=${rand[j]} rand[j]=${rand[j+1]} rand [j + 1 ] = $ var fi done # echo $ {rand [* ] done echo -e "The last array is \t${rand[*]}." echo "The min of the array is ${rand[0]}." echo "The max of the array is ${rand[-1]}."
Demonstration of results.
[root@c7-server ~]# bash arrayBubbleSort.sh The original array is 89 71 5 61 64 81 32 32 44 58. The last array is 5 32 32 44 58 61 64 71 81 89. The min of the array is 5. The max of the array is 89. [root@c7-server ~]# bash arrayBubbleSort.sh The original array is 17 41 81 62 94 97 37 11 11 41. The last array is 11 11 17 37 41 41 62 81 94 97. The min of the array is 11. The max of the array is 97. [root@c7-server ~]# bash arrayBubbleSort.sh The original array is 44 38 38 58 44 36 41 66 85 76. The last array is 36 38 38 41 44 44 58 66 76 85. The min of the array is 36. The max of the array is 85.
After reading the code, we can adjust the echo statement to see the result of each loop sort.
The original array is 57 68 10 50 34 98 52 21 83 36. 57 10 50 34 68 52 21 83 36 98 10 50 34 57 52 21 68 36 83 98 10 34 50 52 21 57 36 68 83 98 10 34 50 21 52 36 57 68 83 98 10 34 21 50 36 52 57 68 83 98 10 21 34 36 50 52 57 68 83 98 10 21 34 36 50 52 57 68 83 98 10 21 34 36 50 52 57 68 83 98 10 21 34 36 50 52 57 68 83 98 10 21 34 36 50 52 57 68 83 98
From the results, we will also find that unless it happens to be the reverse order, the sorting can be completed in the middle of the loop.
This script, I personally think it will be a little bit more difficult, especially for people with weak algorithm foundation and non-programmers. I only knew the result after Google. Those students who really don’t understand, don’t have to struggle, just add the algorithm in the future. This essay is mainly to understand the array.
Question 2: Define an array, the elements of the array are the file names of all files ending with .log in the /var/log/ directory; count the number of lines of the files with an even number and sum them.
#!/bin/bash declare -a filePool=(/var/log/*.log) declare -i sum=0 declare -i line for i in $(seq 0 $((${#filePool[*]}-1))); do if [ $((i%2)) -eq 0 ]; then line=$(wc -l ${filePool[i]} | cut -d " " -f 1) ((sum+=line)) be done echo "The sum of lines which meet the conditions is $sum."