2107/9/15 Friday
today I'm going to share a wave of arrays, so what is an array? An array is a contiguous memory space that stores multiple elements, which is equivalent to a collection of multiple variables, including array names and indexes, and when we create an array we can use the index to do a wave of things, since there are indexes numbered and the arrays are contiguous, then the unordered array is called the sparse array. And our index can also support a custom format, no non-value, 123 to sort, but this feature only bash4.0 can support, the question is, how to view our bash version?
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/06/AC/wKiom1m7uYXiz4nvAABcL8koq7I458.png "title=" 01. PNG "width=" height= "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:270px; "alt=" Wkiom1m7uyxiz4nvaabcl8koq7i458.png "/>
As far as I know, our centos6,7 are all above bash4.0, and CENTOS5 cannot support bash, which is the associated index.
Now that we've mentioned variables, we know that using variables is defined in advance, so how do we define our arrays?
| Declare-a Array_Name |
Array |
| Declare-a Array_Name |
Associative arrays |
Note: The two cannot be converted to each other, and when we delete an element in an array---> Causes sparse formatting
Array_name= ([idx_name1]= ' val1 ' [idx_name2]= ' val2 ' ...)
Note: Associative arrays must be declared before they are called
After the definition is our array assignment, when the assignment is divided into four kinds
(1) Assign only one element at a time;
Array_name[index]=value
weekdays[0]= "Sunday"
Weekdays[4]= "Thursday"
(2) Assign all elements at once:
Array_name= ("VAL1" "VAL2" "VAL3" ...)
(3) Assign only specific elements:
Array_name= ([0]= "VAL1" [3]= "VAL2" ...)
(4) Assignment of interactive array value pairs
Read-a ARRAY
Since we can assign a value, then our query is very easy, using declare-a
There's nothing to say, we'll go straight into the meat scene, quote an array, if it's our reference to an element of a single array, we can use ${array_name[index]} Note: Omitting [INDEX] means referencing an element with subscript 0
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/06/AC/wKiom1m7veKwhMR2AABz9POuLew760.png "title=" 02. PNG "width=" "height=" 332 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:332px; "alt=" Wkiom1m7vekwhmr2aabz9poulew760.png "/>
to reference an element in an array:
Array slice: ${array[@]:offset:number}
Offset: Number of elements to skip
Number: How many elements to remove
All elements after the offset is taken
${array[@]:offset}
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/A5/5C/wKioL1m7vrzzkAEiAAAn8oIP9RI722.png "title=" 03. PNG "width=" "height=" 175 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:175px; "alt=" Wkiol1m7vrzzkaeiaaan8oip9ri722.png "/>
Append some elements to the array:array[${#ARRAY [*]}]=value
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/06/AC/wKiom1m7wAHwzl8oAABSUcFXHUM909.png "title=" 04. PNG "width=" height= "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:160px; "alt=" Wkiom1m7wahwzl8oaabsucfxhum909.png "/>
As the saying goes, the light said no practice false bashi, we have a small problem below, it is our small extension after class:
Exercise: generate 10 random numbers to save in an array and find their maximum and minimum values.
#!/bin/bash
Declare-a Rand
For ((i=0;i<10;i++));d o
rand[$i]= $RANDOM
[$i-eq 0] && max=${rand[$i]} && min= $min && continue
[${rand[$i]}-gt $max] && max=${rand[$i]} && continue
[${rand[$i]}-lt $min] && min=${rand[$i]}
Done
echo "All Rand is ${rand[@]}"
echo "Max is $max"
echo "Min is $min"
Well, today's share to this, the Zhou gang fought 51CTO, content and time are some hasty, insufficient, please advise.
An array of Linux operations learning