Shell Array Summary

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.