1. Array definition
[[email protected] ~]# a= (12345678) [ [Email protected] -IDC ~]# Echo $a
A pair of parentheses indicates an array, and the elements of the array are separated by a "space" symbol.
2. Array reading and Assignment
1) Get the length :
[Email protected] ~]# echo ${#a [@]} 8 [ email protected]-IDC ~]# echo ${#a [*]}8
Use ${#数组名 [@ or *]} to get the array length
2) Read:
[[email protected] ~]# echo ${a[4]}5[[email protected]-IDC ~]# echo ${a[*]} 12345678
Using the ${array name [subscript]} subscript is starting from 0 subscript is: * or @ Get the entire array contents
3) Assign value:
[Email protected] ~]# a[1]= -[[Email protected]-IDC ~]# Echo ${a[*]} 1 - 3 4 5 6 7 8[[Email protected]-IDC ~]# a[5]= $[[Email protected]-IDC ~]# Echo ${a[*]} 1 - 3 4 5 $ 7 8
It can be referenced directly by the array name [subscript], and if the subscript does not exist, a new array element is added automatically
4) Delete:
[Email protected] ~]# a= (1 2 3 4 5 6 7 8) [[email protected]-IDC ~]# unset a[[email protected]-IDC ~]#Echo${a[*]}[[email protected]-IDC ~]# a= (1 2 3 4 5 6 7 8) [[email protected]-IDC ~]# unset a[1][[email protected]-IDC ~]#Echo${a[*]}1 3 4 5 6 7 8[[Email protected]-IDC ~]#Echo${#a [*]}7
Directly through: unset array [subscript] can clear the corresponding element, without subscript, clear the entire data.
3. Special use
1) Shard:
[Email protected] ~]# a= (1 2 3 4 5 6 7 8) [[email protected]-IDC ~]#Echo${a[@]:0:3}1 2 3[[Email protected]-IDC ~]#Echo${a[@]:1:4}2 3 4 5[[Email protected]-IDC ~]# c= (${a[@]:1:4}) [[email protected]-IDC ~]#Echo${#c [@]}4[[Email protected]-IDC ~]#Echo${c[*]} 2 3 4 5
Directly through the ${array name [@ or *]: Start position: Length} Slice the original array, return is a string, the middle with "space" separate, so if you add "()", will get the slice array, the above example: C is a new data.
2) Replace:
[Email protected] ~]# a= (1 2 3 4 5 6 7 8) [[email protected]-IDC ~]#Echo${a[@]/3/ -}1 2 - 4 5 6 7 8[[Email protected]-IDC ~]#Echo${a[@]}1 2 3 4 5 6 7 8[[Email protected]-IDC ~]# a= (${a[@]/3/ -}) [[email protected]-IDC ~]#Echo${a[@]}1 2 - 4 5 6 7 8
The call method is: ${array name [@ or *]/find character/substitution character} This operation will not change the original array contents, if you need to modify, you can see the above example, redefine the data.
4. Intercept the array elements you need
#!/bin/Bashserver=$1Echo '------Starting----------'SERVERS=('192.168.1.10' '192.168.10.10' '192.168.10.11' '192.168.10.12' '192.168.10.13' '192.168.100.100')# forIinch${servers[@]}# Do# Echo "--"$i # Done# [ on]functionlog () {if[ $1=="Info"]; Then Echo-E"\033[32;40m$2\033[0m" elif[ $1=="Error"]; Then Echo-E"\033[31;40m$2\033[0m" elif[ $1=="Debug"]; Then Echo-E"\033[34;40m$2\033[0m" fi}push_server=${servers[0]}proxy_server=${servers[${#SERVERS [*]}-1]}Echo "Push_server ="$PUSH _serverEcho "Proxy_server ="$PROXY _server#Echo "gets the length of the array 1 ="${#SERVERS [@]}unset servers[0]unset servers[${#SERVERS [@]}]#Echo "gets the length of the array 2 ="${#SERVERS [@]}Echo '------------------------------before-------------------'# conditionInstall: Case$SERVERinch #------------------------------------------------------------------------------------# Push Stream distribution Video Server #------------------------------------------------------------------------------------ # [1] Push Flow ######################################################################################"${push_server}") Sleep 1 ;; #------------------------------------------------------------------------------------# Reverse Proxy proxies #------------------------------------------------------------------------------------ ###################################################################################### #"${servers[3]}"gets the last element of the array as a live node CDN"${proxy_server}") Sleep 1 ;; *) Log Debug"------------------current IP-------------------"${server} forIinch${servers[@]} Do Echo "--"$i Done ;;Esac
Linux command Detailed (10) An array of shell scripts