1. Calculate the length of the array:
${#array [@]}
2. List the array index:
${!array[*]} or ${!array[@]}
3. Print all the values in the array:
${array[*]} or ${array[@]}
Iterate through the array elements in 4.shell:
For STR in ${array[@]};
Do
Echo $str;
Done
5. Defining an empty array
Uset Array
Normal array:
1. Initialize:
The first type of initialization
[Email protected] ~/q/data/tmp_data]# array_origin= (1 2 3 4) [[email protected] ~/q/data/tmp_data]# echo ${#array_origin [@]} 4
The second type of initialization
[Email protected] ~/q/data/tmp_data]# arra_origin[0]=00[[email protected] ~/q/data/tmp_data]# arra_origin[1]=11[[ Email protected] ~/q/data/tmp_data]# arra_origin[2]=22[[email protected] ~/q/data/tmp_data]# arra_origin[3]=33
2. Access method:
[[email protected] ~/q/data/tmp_data]# for item in ${arra_origin[@]}; do Echo $item; done00112233
<strong>ps: How to iterate through an array in awk: </strong><span style= "color: #FF6666;" ><strong>i represents the key, not the value, do not avoid it!!</strong></span>
[Email protected] ~/q/data/tmp_data]# echo "1" | awk ' begin{array[1]=11; array[2]=22;array[3]=33;array[4]=44} {for (i in array) {print i}} ' 1234[[email protected] ~/q/dat a/tmp_data]# echo "1" | awk ' begin{array[1]=11; array[2]=22;array[3]=33;array[4]=44} {for (i in array) {print array[i]}} ' 11223344
Associative arrays:
The first type of display declaration is initialized:
[Email protected] ~/q/data/tmp_data]# declare-a ass_array_1
The second inline index value list is initialized:
[[email protected] ~/q/data/tmp_data]# ass_array_2= (["Host1"]= "192.168.1.23" ["Ip1"]= "192.168.1.23")
Assignment Value:
[Email protected] ~/q/data/tmp_data]# ass_array["host"]= "192.168.1.23" [[email protected] ~/q/data/tmp_data]# Ass_ array["IP"]= "192.168.1.23"
visit:
Query value by key
[Email protected] ~/q/data/tmp_data]# echo ${ass_array_1["host"]}192.168.1.23
View all keys
[[email protected] ~/q/data/tmp_data]# echo ${!ass_array_1[*]}host1 IP
View All value
[Email protected] ~/q/data/tmp_data]# echo ${ass_array[*]}192.168.1.23 192.168.1.23
Shell Array Summary