Shell regular arrays & associative arrays

Source: Internet
Author: User

In the process of writing the Shell Daily , the array is a very common content. Here we briefly describe how the two arrays are used. A regular array, an associative array. The difference is a regular array, which can only be accessed using integers as subscripts. Associative arrays can be stored using characters as small labels.

Regular arrays

1) Initialize

Multiple variables in the array are enclosed in parentheses, and the variables are separated by a space between them.

[Email protected]:~/test_shell#./demo1.sh

1 2 3) 4 5

1 2 3 4

[Email protected]:~/test_shell# moredemo1.sh

#!/bin/bash

A= (1 2 3 4 5)

b= (1

2

3

4

)

#Print the Arry

Echo ${a[*]}

Echo ${b[*]}

To save the contents of text in an array as a unit of behavior

[Email protected]:~/test_shell# vim List

[Email protected]:~/test_shell# a= (' catlist ')

[Email protected]:~/test_shell# echo${a[*]}

Line2 line3 line4 line5 line5 line6

[Email protected]:~/test_shell# echo${a[1]}

Line3

[Email protected]:~/test_shell# Cat List

Line2

Line3

Line4

Line5

Line5

Line6

One more thing here is to iterate through every line in the text.

[Email protected]:~/test_shell# whileread line;do echo $line;d one < list

Line2

Line3

Line4

Line5

Line5

Line6

[Email protected]:~/test_shell#./demo1.sh

Do Something:line2

Do Something:line3

Do Something:line4

Do Something:line5

Do Something:line5

Do Something:line6

Do something:

[Email protected]:~/test_shell# moredemo1.sh

#!/bin/bash

Cat List | While Read Line;do

echo "Do Something: $line"

Done

2) data access

To get a specified variable

[Email protected]:~/test_shell# echo${a[1]}

Line3

[Email protected]:~/test_shell# echo${a[2]}

Line4

Get variable All values

[Email protected]:~/test_shell# echo${a[@]}

Line2 line3 line4 line5 line5 line6

[Email protected]:~/test_shell# echo${a[*]}

Line2 line3 line4 line5 line5 line6

[Email protected]:~/test_shell#

Get array variables in reverse

[Email protected]:~/test_shell# echo${a[-2]}

Line5

[Email protected]:~/test_shell# echo${a[-4]}

Line4

Gets the length of the array

[Email protected]:~/test_shell# echo${#a [*]}

6

[Email protected]:~/test_shell# echo${#a [@]}

6

2) Assign & clear a small mark value

[Email Protected]:~/test_shell#echo ${#a [@]}

6

[Email Protected]:~/test_shell#echo ${a[*]}

Line2line3 line4 line5 Line5 line6

[Email Protected]:~/test_shell#unset a[3]

[Email Protected]:~/test_shell#echo ${a[*]}

Line2line3 line4 Line5 Line6

[Email Protected]:~/test_shell#echo ${#a [@]}

5

3) shard Access

${Array name[@or*]:Start Position:length}Slice the original array, return is a string

[Email Protected]:~/test_shell#echo ${a[@]:1:1}

Line3

[Email Protected]:~/test_shell#echo ${a[@]:1:2}

Line3 line4

4) Replace content

[Email Protected]:~/test_shell#echo ${a[*]}

Line2line3 line4 Line5 Line6

[Email Protected]:~/test_shell#echo ${a[*]/2/test}

Linetestline3 line4 Line5 Line6

[Email Protected]:~/test_shell#echo ${a[*]/2/test}

Line test Line3 line4 line5 line6

[Email protected]:~/test_shell#b= (${a[*]/2/test})

[Email Protected]:~/test_shell#echo $b

Line

[Email Protected]:~/test_shell#echo ${b[@]}

Line Testline3 line4 Line5 line6

[Email Protected]:~/test_shell#echo ${a[@]}

Line2 line3 line4 Line5 line6

Associative arrays

Declare reference links :

http://blog.csdn.net/tutuboke/article/details/50440598

The difference between an associative array and a regular array is that associative arrays can be used as strings. Other methods of operation and regular arrays are the same, can be fragmented, can be eliminated.

Initialization

[Email protected]:~ #declare-A Aa_array

[Email protected]:~ #aa_array [' index1 ']= ' value1 '

[Email protected]:~ #aa_array [' index2 ']= ' value2 '

[Email protected]:~ #aa_array [' index3 ']= ' value3 '

[Email protected]:~ #echo ${aa_array[@]}

Value1 value2 Value3

Another way to assign a value

[Email protected]:~ #declare-A a_array= ([' I1 ']= ' v1 ' [' i2 ']= ' v2 ' [' i3 ']= ' v3 ')

[Email protected]:~ #echo ${a_array[@]}

V3 V2 v1



This article is from the "Start from scratch" blog, be sure to keep this source http://atong.blog.51cto.com/2393905/1912203

Shell regular arrays & associative arrays

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.