Bash Shell Basic features three (array)
One, the array definition
An array is a contiguous number of independent memory spaces, each of which corresponds to a variable. The bash shell supports only one-dimensional arrays, but there is no limit to the number of arguments.
Array elements: Array name + index (numbering starting from 0)
Declaring an array: declare-a Arrar_name (You don't have to declare it, you assign it directly to the variable as an array, and bash knows it's an array)
Associative array: declare-a array_name
Assignment of array elements: Assign all elements at once, assign values at the specified index, assign only one element at a time
(1) array= (var1 var2 var3 ... varn)
(2) array= ([0]=VAR1 [1]=var2] [2]=var3 ... [N]=varn]
(3) Array[0]=var1
Arrya[1]=var2
...
Second, array operation
1. Referencing array elements: ${array[index]}
2. Count the number of array elements: ${#array [@]} or ${#array [*]}
Note: Bash's special parameters @ and * Both denote "extended position parameters, starting from 1", but the form is slightly different, but it seems to be common in arrays.
Example 1): Scripting array01.sh, Reading n strings from "standard input", each time the input string is saved in an array of arrays
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/76/17/wKiom1ZJzYCRBAWFAAC9zERaP3g018.jpg "style=" float: none; "title=" 2-1.jpg "alt=" Wkiom1zjzycrbawfaac9zerap3g018.jpg "/>
650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M00/76/14/wKioL1ZJzdLhBzGqAAEkCtjTs-k981.jpg "style=" float: none; "title=" 2-2.jpg "alt=" Wkiol1zjzdlhbzgqaaekctjts-k981.jpg "/>
Example 2): array02.sh, place the letters in the string one by one into the array and output to "standard output"
650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M00/76/14/wKioL1ZJzeXTPm-XAAFkLdw4SY8546.jpg "style=" float: none; "title=" 2-3.jpg "alt=" Wkiol1zjzextpm-xaafkldw4sy8546.jpg "/>
650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M01/76/14/wKioL1ZJzeexj1-KAAE1GNAqqjA482.jpg "style=" float: none; "title=" 2-4.jpg "alt=" Wkiol1zjzeexj1-kaae1gnaqqja482.jpg "/>
3. Display the entire array or an array element:
# echo ${season[*]} or echo ${season[@]}
# echo ${season[3]}
4. Clears the specified single array element or clears the entire array:
# unset Season[2]
# unset SEASON
5. String manipulation:
Array element match removal (in element, for all elements)
#: Shortest match from left to right
# #: Longest match from left to right
%: Shortest match from right to left
Percent: longest match from right to left
${#string}: Returns the length of the $string
such as: # netpath= "Sysconfig/network-scripts/ifcfg-eth"
# echo ${#netpath}
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/76/14/wKioL1ZJzf3y11EpAACiT-f8u3Q712.jpg "title=" 2-5. JPG "alt=" wkiol1zjzf3y11epaacit-f8u3q712.jpg "/>
${string#*word}:word can be any character, from left to right, to find the characters stored in the sting variable, the first occurrence of word, the deletion of all characters from the beginning of the character until the first word appears at the same time
such as: # netpath= "Sysconfig/network-scripts/ifcfg-eth"
# echo ${netpath#*/}
650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M01/76/14/wKioL1ZJzgqj3aIAAABlPnD6o-0124.jpg "title=" 2-6. JPG "alt=" wkiol1zjzgqj3aiaaablpnd6o-0124.jpg "/>
${string##*word}:word can be any character, from left to right, to find the characters stored in the sting variable, the first occurrence of word, the deletion of all characters from the beginning of the character until the last word appears at the same time
such as: # netpath= "Sysconfig/network-scripts/ifcfg-eth"
# echo ${netpath##*/}
650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M02/76/14/wKioL1ZJzhagpenMAABggZGDsVk287.jpg "title=" 2-7. JPG "alt=" wkiol1zjzhagpenmaabggzgdsvk287.jpg "/>
${string%word*}:word can be any character, from right to left, looking for characters stored in the sting variable, deleting all characters from the beginning of the character until the first word appears
such as: # netpath= "Sysconfig/network-scripts/ifcfg-eth"
# echo ${netpath%f*}
650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/76/14/wKioL1ZJziHxTFh7AABjJV5gTA0510.jpg "title=" 2-8. JPG "alt=" wkiol1zjzihxtfh7aabjjv5gta0510.jpg "/>
${string%%word*}:word can be any character, from right to left, to find the characters stored in the sting variable, and to delete all characters from the beginning of the character until the last word appears.
such as: # netpath= "Sysconfig/network-scripts/ifcfg-eth"
# echo ${netpath%%f*}
650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M02/76/17/wKiom1ZJzdzh7QrDAAByRXiSzy4962.jpg "title=" 2-9. JPG "alt=" wkiom1zjzdzh7qrdaabyrxiszy4962.jpg "/>
Array element Match substitution
/XX/YY: Replace only once for each element
XX/YY: Replace each element multiple times
x/: Delete Matching content
such as: #array = (Abe abc abcee ABCD Abde)
#echo ${array[@]/e/e} # Abcee second e not replaced
#echo ${array[@]//e/e} # Abcee Second E is replaced
# echo ${array[@]//e/} # max Match delete
# echo ${array[@]/e/} # minimum Match delete
650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/76/17/wKiom1ZJzemTmcCzAAGUajvd4Hc963.jpg "title=" 2-10. JPG "alt=" wkiom1zjzemtmcczaaguajvd4hc963.jpg "/>
/#xx/yy Front-End match (left to right)
/%xx/yy Back-end Match (right-to-left)
such as: #echo ${array[@]/#a/A} # ABC's a not replaced
#echo ${ARRAY[@]/%A/A} # ABC's a not replaced
650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M00/76/14/wKioL1ZJzkXAdM9hAAC-owx5i0Y806.jpg "title=" 2-11. JPG "alt=" wkiol1zjzkxadm9haac-owx5i0y806.jpg "/>
adding elements
such as: #array = (Abe abc abcee ABCD Abde)
#array = ("${array[@]}" "BBBBB")
#echo ${array[@]}
650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M00/76/17/wKiom1ZJzgDDirEpAAC0ppdDjTY937.jpg "title=" 2-12. JPG "alt=" wkiom1zjzgddirepaac0ppddjty937.jpg "/>
Third, practice
1, write a script array03.sh: Define an array, the array element is the name of all the files in the/var/log directory that end with. log; Displays the number of rows per file;
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M00/76/14/wKioL1ZJzmDQbqYrAALTguq0soY478.jpg "title=" 2-13. JPG "alt=" wkiol1zjzmdqbqyraaltguq0soy478.jpg "/>
2, write a script array04.sh, generate 10 random numbers, and according to the order from small to large;
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/76/14/wKioL1ZJzm-CNNnNAAFZPrncpi4604.jpg "title=" 2-14. JPG "alt=" wkiol1zjzm-cnnnnaafzprncpi4604.jpg "/>
3, write a script, can randomly select a classmate from all the students to answer questions; further: can accept a parameter, as the number of students to select;
This article is from the "10,000-hour Law" blog, be sure to keep this source http://daisywei.blog.51cto.com/7837970/1713240
Bash Shell Basic features three (array)