In bash shell, the handling of arrays is an error-prone place.
Array:
Variable: Stores the memory space of a single element;
Array: A contiguous memory space that stores multiple elements;
Array name
Index: Numbering starting from 0, is a numeric index;
Note: Indexes can also support the use of custom formats, not just numeric formats;
The bash array supports sparse formats, but does not store each number of elements in contiguous memory space
Declaring an array:
Declare-a Array_Name
Declare-a array_name: Associative array;
-
array element assignment:
(1) assigns only one element at a time;
Array_name[index]=value
weekdays[0]= "Sunday"
weekdays[4]= "Thursday"
(2) assigns all elements at once:
array_name= ("VAL1" "VAL2" "VAL3" ...)
(3) assigns only specific elements:
array_name= ([0]= "VAL1" [3]= "VAL2" ...)
(4) read-a ARRAY
-
References array elements: ${array_name[index]}
Note: Omit [INDEX] to refer to the element labeled 0;
650) this.width= 650; "Src=" http://s3.51cto.com/wyfs02/M02/73/17/wKioL1X1AFqR22ImAAETJ70IIJE344.jpg "title=" 282.png "alt=" Wkiol1x1afqr22imaaetj70iije344.jpg "/>
the length of the array (the number of elements in the array): ${#ARRAY_NAME [*]}, ${#ARRAY_NAME [@]}  &NBSP
650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M00/73/17/ Wkiol1x1atmychvwaaa1thqksxo064.jpg "style=" Float:none; "title=" 283.png "alt=" wkiol1x1atmychvwaaa1thqksxo064.jpg "/
Example: Generate 10 random numbers to save in an array and find their maximum and minimum values;
#!/bin/bash
#
Declare-a Rand
Declare-i max=0
For i in {0..9}; Do
rand[$i]= $RANDOM
echo ${rand[$i]}
[${rand[$i]}-gt $max] && max=${rand[$i]}
Done
echo "Max: $max"
Example: Define an array in which the elements in the array are all files ending in. Log in the/var/log directory, and the sum of the number of rows in the file to be labeled as even;
#!/bin/bash
#
declare-a files
Files= (/var/log/*.log)
Declare-i lines=0
For I in $ (seq 0 $[${#files [*]}-1]); Do
If [$[$i%2]-eq 0];then
Let lines+=$ (Wc-l ${files[$i]} | cut-d "-F1)
Fi
Done
echo "Lines: $lines."
To reference an element in an array:
All elements: ${array[@]}, ${array[*]}
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/17/wKioL1X1AtmSChZLAAA9wq4eVa0088.jpg "style=" float: none; "title=" 284.png "alt=" Wkiol1x1atmschzlaaa9wq4eva0088.jpg "/>
Array slice: ${array[@]:offset:number}
Offset: Number of elements to skip
Number: The amount of elements to be fetched, all elements after the offset: ${array[@]:offset};
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/17/wKioL1X1BXvQKJAHAAB4ji422sw900.jpg "style=" float: none; "title=" 285.png "alt=" Wkiol1x1bxvqkjahaab4ji422sw900.jpg "/>
Append an element to the array:
array[${#ARRAY [*]}]
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/1A/wKiom1X1A0myNOBUAAA8xY2muLE449.jpg "style=" float: none; "title=" 286.png "alt=" Wkiom1x1a0mynobuaaa8xy2mule449.jpg "/>
To delete an element in an array:
Unset Array[index]
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/17/wKioL1X1BXuDOhU7AAA3gA2KBpY085.jpg "style=" float: none; "title=" 287.png "alt=" Wkiol1x1bxudohu7aaa3ga2kbpy085.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/1A/wKiom1X1A0mjnUIJAABGae4zey8496.jpg "style=" float: none; "title=" 288.png "alt=" Wkiom1x1a0mjnuijaabgae4zey8496.jpg "/>
Associative arrays:
Declare-a Array_Name
Array_name= ([index_name1]= ' val1 ' [index_name2]= ' val2 ' ...)
Exercise: Generate 10 random numbers, ascending or descending sort;
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/1A/wKiom1X1A7qi5uWRAACy2OUM0O4677.jpg "title=" 289. PNG "alt=" wkiom1x1a7qi5uwraacy2oum0o4677.jpg "/>
This article is from the "After Tomorrow" blog, please be sure to keep this source http://leeyan.blog.51cto.com/8379003/1694252
Bash Shell Array