Linux Shell Script Raiders (1.7)

Source: Internet
Author: User

1.7 arrays and associative arrays
    • An array is a very important part of Shell scripting, and it uses indexes to store multiple independent independent data as a collection. An ordinary array can use integers only as an array index, and associative arrays not only use integers as indexes, but they can also use strings as indexes. In general, it is easier to understand the use of strings for indexing. Bash introduces associative arrays from 4.0 onwards.
1.7.1 definition print normal array
    • There are several methods of array:
#在一行上列出所有元素array_var=(123456)#以“索引-值”的形式一一列出array_var[0]="test1"array_var[1]="test2"array_var[2]="test3"
    • Note: The first method uses parentheses, otherwise it will be followed by an error.

    • There are several methods of array elements:

echo${array_var[0]}         #输出结果为 test1index=2echo${array_var[$index]}    #输出结果为 test3echo${array_var[*]}         #输出所有数组元素echo${array_var[@]}         #输出所有数组元素echo${#array_var[*]}        #输出值为 3
    • Note: in Ubuntu 14.04, the shell script starts with #!/bin/bash and executes the script in the same way as Bash test.sh.
1.7.2 Define print associative array
    • Defining associative arrays
      In an associative array, you can use any text as an array index. When defining an associative array, you first need to declare a variable as an associative array using a declaration statement, and then you can add elements to the array, as follows:
Declare-A Ass_array#声明一个关联数组Ass_array= (["Index1"]=INDEX1 ["Index2"]=INDEX2)#内嵌 the "index-value" list methodass_array["Index3"]=index3ass_array["Index4"]=index4Echo ${ass_array["Index1"]}                    #输出为index1Echo ${ass_array["index4"]}Echo ${!ass_array[*]}                          #输出索引列表Echo ${!ass_array[@]}                          #输出索引列表
    • Note: for normal arrays, the index list can still be listed using the above method, and you cannot add the dollar sign before declaring an associative array and adding array elements.
1.7.3 Reference
    • Linux Shell Script Raiders

Linux Shell Script Raiders (1.7)

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.