Linux Foundation Bash script Advanced-array

Source: Internet
Author: User



Array


What is an array?


An array in a computer refers to a data type that is organized in a certain order by a number of variables of the same type.


The popular point is that the array is one of the variables, in terms of the current term is a variable circle, but this variable circle is the same type of variables and have a certain organizational order.



Format of the array


Array[key]=value


Array: Represents the name of the array variable


Key: Indicates the index of the array element is also called subscript


Value: Values representing the array elements corresponding to the key



Basically understand what an array is, here's how to define an array or how to create an array



—————————————————————————————————————————————

Defining arrays


There are several types of definition arrays:


1. Defining an array by specifying elements

#!/bin/bash

#通过指定元素来定义数组

#指定数组元素值

Array[0]=8

Array[2]=two

#输出数组元素

echo "${array[@]}"


Run the script


[[Email protected] test]# sh 20160910-1

8


2. Defining arrays by Declare statements

#!/bin/bash

#通过declare语句来定义数组

The #定义数组-a option indicates that an array, named array, is defined later.

Declare-a Array

#为元素赋值

Array[0]=1

array[1]=2

#输出元素值

echo "${array[@]}"


Run the script

[[Email protected] test]# sh 20160910-2

1 2


With the above two examples, it can be found that such declarations in the shell are not very necessary , because in the shell, all variables do not have to show the definition to be used as an array, in a sense all variables are arrays : assigning to a variable with no subscript is the same as assigning a value to an element in the subscript 0.


Example:

#!/bin/bash

Tom=1

echo "${tom}"

echo "${tom[0]}"


Run the script

[[Email protected] test]# sh 20160910-3


The result shows that the assignment to a variable with no subscript is the same as assigning a value to an element in the subscript 0.



3. Defining Arrays by Element list


#!/bin/bash

#通过元素列表定义数组

#定义数组

Array= (1 2 3 4 5 6)

#输出第4个数组元素的值

echo "The fourth element is ${array[3]}"

#输出所有元素的值

echo "The elements of this array is ${array[@]}"


Run the script

[[Email protected] test]# sh 20160910-4

The fourth element is 4

The elements of this array is 1 2 3 4 5 6



—————————————————————————————————————————————

Access or view of an array


There are several ways to access an array, and here are two common types:


1. View its corresponding element value by index value

#!/bin/bash

#定义数组

Array= (1 2 3)

#查看下标为1的元素

echo "The second element is ${array[1]}"


Run the script

[[Email protected] test]# sh 20160910-5

The second element is 2



2. Iterating through the array through a loop list


If we want to use a for-loop c expression, then there is a problem to solve, is to take a range of values, and how many elements of the array is only defined by the person to know, if there is a ready-made array, we do not know how many elements of the case, how to iterate through a for loop display?


For this we need to know how many elements of the array, in the array can be represented by ${#array [@]}


#!/bin/bash

#通过循环列表定义数组

#定义数组

Array= (one three four five 6 7 8 9 10)

#获取数组长度

Length= "${#array [@]}"

#通过循环遍历数组

For ((i=0;i< $length; i++));d o

echo "${array[$i]}"

Done


Run the script

[[Email protected] test]# sh 20160910-6

One

Both

Three

Four

Five

6

7

8

9

10


—————————————————————————————————————————————

Associative arrays


An associative array is an array that has a special indexed way. You can index it not only by integer, but also by using a string or other type of value (except null).


Associative arrays must be declared beforehand , otherwise the creation of associative arrays fails


declare-a Array Declaration associative arrays


array= ([A]=tom [B]=john [c]=3]


If we define the subscript as the name of the person whose corresponding element is the result, then enter the specified name to know its corresponding score


#!/bin/bash

#定义关联数组

DECLARE-A Grades

#为数组下标指定对应的分数

grades= ([tom]=90 [lilei]=88] [rose]=92 [lucy]=80]

#给一个名字

Read-p "Give a name:" Name_g

#判断是否给了内容, if not, exit at the prompt

[-Z $name _g] && echo "None" Please select [tom|lilei|rose| Lucy] && exit 1 "

#根据输入的名字输出对应的分数

Case $name _g in

Tom

echo "Tom ' s grades is ${grades[tom]}";

Lilei)

echo "Lilei ' s grades is ${grades[lilei]}";

Rose

echo "Rose ' s grades is ${grades[rose]}";

Lucy)

echo "Lucy ' s grades is ${grades[lucy]}";

*)

echo "Please select [tom|lilei|rose| Lucy] "

Esac


Run the script

[[Email protected] test]# sh 20160910-7

Give a Name:tom

Tom ' s grades is 90

[[email protected] test]# vim 20160910-7

[[Email protected] test]# sh 20160910-7

Give a Name:rose

Rose ' s grades is 92

[[Email protected] test]# sh 20160910-7

Give a Name:lilei

Lilei ' s grades is 88

[[Email protected] test]# sh 20160910-7

Give a Name:lucy

Lucy ' s grades is 80

[[Email protected] test]# sh 20160910-7

Give a Name:other

Please select [tom|lilei|rose| Lucy]


—————————————————————————————————————————————

Array additions and deletions



Increase the number of arrays


#!/bin/bash

#定义数组

Array= (1 2)

#显示数组所有元素

echo "${array[@]}"

#显示数组元素个数

echo "${#array [@]}"

#追加数组元素

Array[2]=3

Array[3]=4

#显示追加后的数组信息

echo "${array[@]}"

echo "${#array [@]}"


Run the script


[[Email protected] test]# sh 20160910-8

1 2

2

1 2 3 4

4


You can start with any unused subscript number in the array. Arrays can be added noncontiguous



Delete an array


1. Delete an array to specify an element


Command: unset array[n]

#!/bin/bash

#定义数组

Array= (1 2 3)

#显示数组所有元素

echo "${array[@]}"

#显示下标为0的数组元素

echo "${array[0]}"

#删除下标为0的数组元素

Unset Array[0]

echo "${array[@]}"

echo "${array[0]}"



2. Delete the entire array


Command: unset array


#!/bin/bash

#定义数组

Array= (1 2 3)

echo "${array[@]}"

#删除整个数组

unset array

echo "${array[@]}"


Run the script

[[Email protected] test]# sh 20160910-9

1 2 3



Merging between arrays


An array can synthesize several different array groups into a new array.


The array connection syntax is as follows:


("${array1[@]}" "${array2[@]}" ... "${arrayn[@]}")


Separating arrays with spaces


Example

#定义两个数组

Array1= (1 2 3)

Array2= (4 5 6)

#连接数组

Array1_2= ("${array1[@]}" "${array2[@]}")

#显示合并后数组的所有元素

echo "${array1_2[@]}"


Run the script

[[Email protected] test]# sh 20160910-10

1 2 3 4 5 6


—————————————————————————————————————————————

Slices in the array


A slice is a section of an element or part of an element that intercepts an array.


The basic syntax is: ${array[@|*|n]:start:length}


Example 1: Given an array of length 8, intercept the 4 elements that begin with a 3rd element.

#!/bin/bash

Array= (1 2 3 4 5 6 7 8)

Var=${array[@]:3:4}

echo "$var"


Run the script


[[Email protected] test]# sh 20160910-11

4 5 6 7



Example 2: Given an array of length 3, intercept the 2nd element, start with the 1th character of the element, and intercept 3 characters

#!/bin/bash

array= (Apple orange Banana)

Var=${array[2]:1:3}

echo "$var"


Run the script

[[Email protected] test]# sh 20160910-12

Ana


—————————————————————————————————————————————

The substitution in the array


Substitution of an array is the substitution of parts of an array element with other strings, but does not affect the value of the original array.


The basic syntax is: ${array[@|*]/pattern/replacement}

#!/bin/bash

Replace= (a b c D e)

echo "The replaced array is ${REPLACED[@]/C/3}"

echo "The original array is ${replace[@]}"


Run the script

[[Email protected] test]# sh 20160910-13

The replaced array is a B 3 d E

The original array is a B c d E


————————————————————————————————————————————

Summary


This article mainly deals with the concept of arrays, the definition of arrays, the view of arrays, the use of associative arrays, the additions and deletions of arrays, the array of slices, and the substitution of these 7 parts.


feeling : After summing up the array, I vaguely sensed that there was a certain correlation between the array and the database.


View history knowing that the array was born before the database, I felt that the idea of the database was to some extent a reference to some of the ideas of arrays. Of course the database is much stronger than the array, in the data query search professional many. But there is a fundamental agreement between the two: based on the correspondence between the data.



This article is from the "eyes engraved with your Smile" blog, please be sure to keep this source http://dengaosky.blog.51cto.com/9215128/1852296

Linux Foundation Bash script Advanced-array

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.