Linux Foundation Bash script Advanced-array

Source: Internet
Author: User
Tags array definition

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 an array variable

Key: indicates that the index of an array element is also called subscript

Value: value representing the array element 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# the array element value by specifying an element # array[0]=8array[2]=two# output array element echo "${array[@]}"

Run the script

[Email protected] test]# sh 20160910-18

2. Defining arrays by Declare statements

#!/bin/bash# defining an array by using the Declare Statement array-A option means that an array named array is defined later. Declare-a array# for element assignment array[0]=1array[1]=2# output element value echo "${array[@]}"

Run the script

[[Email protected] test]# SH 20160910-21 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/bashtom=1echo "${tom}" echo "${tom[0]}"

Run the script

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

The result shows that the assigned value A variable with no subscript is the same as an element that is assigned a value of 0 in the subscript.


3. Defining Arrays by Element list

#!/bin/bash# array Definition array by element list array= (1 2 3 4 5 6) #输出第4个数组元素的值echo "The fourth element is ${array[3]}" #输出所有元素的值echo "the Eleme NTS of this array is ${array[@]} "

Run the script

[[Email protected] test]# sh 20160910-4the fourth element is 4The elements of this array was 1 2 3 4 5 6


Access or view of an array

There are several ways to access an array, and the following are two common examples of :

1. View its corresponding element value by index value

#!/bin/bash# definition Array array= (1 2 3) #查看下标为1的元素echo "The second element is ${array[1]}"

Run the script

[Email protected] test]# sh 20160910-5the 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# defines an array by looping through the list # definition array array= (one three four five 6 7 8 9) #获取数组长度length = "${#array [@]}" #通过循环遍历数组for ((i=0;i < $length, i++));d o echo "${array[$i]}" done

Run the script

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


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 declaring 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# defines an associative array declare-a grades# assigns a corresponding score grades= ([tom]=90 [lilei]=88] [rose]=92 [lucy]=80] #给一个名字read-P "for an array subscript Give A Name: "name_g# determine if the content is given, if not then exit at the prompt [-Z $name _g] && echo" None "Please select [tom|lilei|rose| Lucy] && exit 1 "#根据输入的名字输出对应的分数case $name _g Intom) 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-7give a name:tomtom ' s grades is 90[[email protected] test]# vim 20160910-7[[email Protected] test]# sh 20160910-7give a name:roserose ' s grades is 92[[email protected] test]# sh 20160910-7give a name:l Ileililei ' s grades is 88[[email protected] test]# sh 20160910-7give a Name:lucylucy ' s grades is 80[[email protected] tes t]# sh 20160910-7give a name:otherplease select [tom|lilei|rose| Lucy]


Array additions and deletions


Increase the number of arrays

#!/bin/bash# definition Array array= (1 2) #显示数组所有元素echo "${array[@]}" #显示数组元素个数echo "${#array [@]}" #追加数组元素array [2]=3array[3]=4# Displays the appended array information echo "${array[@"} "echo" ${#array [@]} "

Run the script

[[Email protected] test]# sh 20160910-81 221 2 3 44

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# definition Array 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# definition Array array= (1 2 3) echo "${array[@]}" #删除整个数组unset Arrayecho "${array[@]}"

Run the script

[[Email protected] test]# SH 20160910-91 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-101 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/basharray= (1 2 3 4 5 6 7 8) Var=${array[@]:3:4}echo "$var"

Run the script

[Root[email protected] test]# sh 20160910-114 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/basharray= (Apple orange banana) Var=${array[2]:1:3}echo "$var"

Run the script

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

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/bashreplace= (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-13the replaced array is a B 3 D EThe original array is a B c d E


This article is from "Zhang Fan-it's fantasy drifting" blog, please be sure to keep this source http://chawan.blog.51cto.com/9179874/1851443

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.