Shell Programming Array Summary

Source: Internet
Author: User

Array Summary

Directory:

    1. Array composition

    2. Array Assignment

    3. Array output

    4. Array case

1. Array composition

The composition of an array is a collection of elements, using a variable to store multiple elements, to prevent an element from taking a variable resulting in a large number of variables, the array consists of array names (variables), elements (variable values) and Arrays subscript Group, read an array with the syntax structure: ${variable name [index number]}, It is equivalent to the {$ variable name [1], $ variable name [2]. $ variable name [n]}.

The classification of the array is divided into two main categories, the first class is a normal array, the ordinary array index number is continuous, declaring that the normal array with the syntax structure: delcare–a array name. The second class is an associative array, which is an array of discontinuous index numbers, stating that the associative array takes the syntax structure: declare–a array name.

2. Assigning values to arrays

The copy of a variable is like a loop value in a For loop, and can be assigned in several ways, as follows:

Method 1: Assign the integer list direct value to the array by parentheses

#!/bin/bash

Declare-a arr

Arr= (1 2 3) #将定义整数列表赋值

For i in {0..2};d o

echo "This is the $i times"

echo "value=${arr[$i]}"

Done

Method: 2: By the command return value

[[email protected] ~/txt] #vim array.sh

#!/bin/bash

Arr= ($ (ls/root/txt/))

For i in {0..2};d o

echo "This is the $i times"

echo "value=${arr[$i]}"

Done

[email protected] ~/txt]#./array.sh # execution script Displays the script name, just the script name.

This is the 0 times

Value=1.txt

This is the 1 times

Value=2.txt

This is the 2 times

Value=3.txt

Method 3: Assign a value by using a wildcard character

[[email protected] ~/txt] #vim array.sh

#!/bin/bash

Declare-a arr1

arr1= (/root/txt/*.txt) #数组赋值文件, processing the file

For j in {0..2};d o

echo "This is the $j times"

[-F ${arr1[$j]}] && echo Zunzai | | Echo Bucunzai #通过数组元素判断文件是否存在

Lines= ' Cat ${arr1[$j]} |wc-l ' #统计每个文件的行号

Let sum+= $lines

Echo $sum

echo "value=${arr1[$j]}" #通过数组显示文件名称

Done

[Email protected] ~/txt]#./array.sh #验证执行结果

This is the 0 times

This is first Txt.word

Value=1.txt

This is the 1 times

This is Sencond Txt.word

Value=2.txt

This is the 2 times

This is three Txt.word

Value=3.txt

3. Array output

The array is equivalent to a set of variable elements that, when output, can output the specified element, the total number of elements and elements of the output:

1. the output of the whole element, the syntax structure for the ${variable name [*|@]}, where *|@ represents the wildcard character arbitrary meaning, so will output all the elements.

[[email protected] ~/txt] #intarr = (1 2 3)

[[email protected] ~/txt] #echo ${intarr[*]}

1 2 3

2. Output The specified element, using the syntax structure as the ${variable name [index number]}, where the index number starts from 0

[[email protected] ~/txt] #intarr = (1 2 3)

[[email protected] ~/txt] #echo ${intarr[0]}

1

[[email protected] ~/txt] #echo ${intarr[1]}

2

3. Span style= "font-size:16px;line-height:150%;font-family: ' Imitation ';" > Modify the elements of one of the array variables in the array or add an element, using the syntax structure: variable name [index number]=***, when the index number of the variable exists, overwrite the original value of the variable element, if there is no index number of the variable, add the new variable element in the array.

[[email  protected]  ~/txt] #intarr [1]=20     #存在索引编号1, so overwrite the original value

20

[[email protected]  ~/txt] #intarr [3]=40      #不存在索引编号3, so new variable value

[[email  Protected]  ~/txt] #echo ${intarr[*]}

1 20 3 40

4. Span style= "font-size:16px;line-height:150%;font-family: ' Imitation ';" > Delete an element in the array using the syntax structure: unset array [index number], when deleting the entire array: unset array.

[[email protected] ~/txt] #echo ${intarr[*]}     #打印真个数组

1 3 +

[[email protected] ~/txt] #unset intarr[3]      #删除数组中第4个元素

[[email protected] ~/txt] #echo ${intarr[*]}

1 3

[[email protected] ~/txt] #unset intarr          #删除真个数组

[[email protected] ~/txt] #echo ${intarr[*]}

     5.

[[email  protected] ~/txt] #intarr = ({a). Z})                 #生成序列

a b c d E F g h i j k l m n o p q R S t u v w x y z

[[email&n Bsp;protected] ~/txt] #echo ${intarr[@]:2:2}     #取第2个元素后面的两个元素

     6.

[[email protected] ~/txt] #echo ${intarr[*]}

a b c D E F g h i j k l m n o p q R s t u V W x y z

b b c D E F g h i j k l m n o p q R S t u v W x y z 

4. Array cases

1. Loop Print set element, array contains IP address: 192.168.1.110.1.1.1 172.16.0.1;

#!/bin/bash

Declare-a Ip_arr

Declare-i i=0

Declare-i J

Ip_arr= (#定义数组, the array element is an IP address, fully stating that the array is the set of variables

192.168.1.1

10.1.1.1

172.16.0.1

)

###############################################

echo " Method 1 Loop Print group element "

For I in $ (seq 0 $[${#ip_arr [*]}-1]);d o

echo ${ip_arr[$i]}

Let i++

Done

###############################################

echo " Method 2 Loop Print group element "

For ((j=0;j<${#ip_arr [*]};j++))

Do

echo ${ip_arr[$j]}

Done

################## #将数组作为for循环中的元素进行循环 ##########################

echo " Method 3 array Traversal print array element "

For n in ${ip_arr[*]};d o

Echo $n

Done


This article is from the "11831715" blog, please be sure to keep this source http://11841715.blog.51cto.com/11831715/1962177

Shell Programming 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.