Linux command Detailed (10) An array of shell scripts

Source: Internet
Author: User
Tags array definition array length

1. Array definition

[[email protected] ~]# a= (12345678) [ [Email protected] -IDC ~]# Echo $a

A pair of parentheses indicates an array, and the elements of the array are separated by a "space" symbol.

2. Array reading and Assignment
1) Get the length :

[Email protected] ~]# echo ${#a [@]} 8 [ email protected]-IDC ~]# echo ${#a [*]}8

Use ${#数组名 [@ or *]} to get the array length

2) Read:

[[email protected] ~]# echo ${a[4]}5[[email protected]-IDC ~]# echo ${a[*]}  12345678

Using the ${array name [subscript]} subscript is starting from 0 subscript is: * or @ Get the entire array contents

3) Assign value:

[Email protected] ~]# a[1]= -[[Email protected]-IDC ~]# Echo ${a[*]} 1  - 3 4 5 6 7 8[[Email protected]-IDC ~]# a[5]= $[[Email protected]-IDC ~]# Echo ${a[*]} 1  - 3 4 5  $ 7 8

It can be referenced directly by the array name [subscript], and if the subscript does not exist, a new array element is added automatically

4) Delete:

[Email protected] ~]# a= (1 2 3 4 5 6 7 8) [[email protected]-IDC ~]# unset a[[email protected]-IDC ~]#Echo${a[*]}[[email protected]-IDC ~]# a= (1 2 3 4 5 6 7 8) [[email protected]-IDC ~]# unset a[1][[email protected]-IDC ~]#Echo${a[*]}1 3 4 5 6 7 8[[Email protected]-IDC ~]#Echo${#a [*]}7

Directly through: unset array [subscript] can clear the corresponding element, without subscript, clear the entire data.

3. Special use
1) Shard:

[Email protected] ~]# a= (1 2 3 4 5 6 7 8) [[email protected]-IDC ~]#Echo${a[@]:0:3}1 2 3[[Email protected]-IDC ~]#Echo${a[@]:1:4}2 3 4 5[[Email protected]-IDC ~]# c= (${a[@]:1:4}) [[email protected]-IDC ~]#Echo${#c [@]}4[[Email protected]-IDC ~]#Echo${c[*]} 2 3 4 5

Directly through the ${array name [@ or *]: Start position: Length} Slice the original array, return is a string, the middle with "space" separate, so if you add "()", will get the slice array, the above example: C is a new data.

2) Replace:

[Email protected] ~]# a= (1 2 3 4 5 6 7 8) [[email protected]-IDC ~]#Echo${a[@]/3/ -}1 2  - 4 5 6 7 8[[Email protected]-IDC ~]#Echo${a[@]}1 2 3 4 5 6 7 8[[Email protected]-IDC ~]# a= (${a[@]/3/ -}) [[email protected]-IDC ~]#Echo${a[@]}1 2  - 4 5 6 7 8

The call method is: ${array name [@ or *]/find character/substitution character} This operation will not change the original array contents, if you need to modify, you can see the above example, redefine the data.

4. Intercept the array elements you need

#!/bin/Bashserver=$1Echo '------Starting----------'SERVERS=('192.168.1.10' '192.168.10.10' '192.168.10.11' '192.168.10.12' '192.168.10.13' '192.168.100.100')# forIinch${servers[@]}# Do#  Echo "--"$i # Done# [ on]functionlog () {if[ $1=="Info"]; Then        Echo-E"\033[32;40m$2\033[0m"    elif[ $1=="Error"]; Then        Echo-E"\033[31;40m$2\033[0m"    elif[ $1=="Debug"]; Then        Echo-E"\033[34;40m$2\033[0m"    fi}push_server=${servers[0]}proxy_server=${servers[${#SERVERS [*]}-1]}Echo "Push_server ="$PUSH _serverEcho "Proxy_server ="$PROXY _server#Echo "gets the length of the array 1 ="${#SERVERS [@]}unset servers[0]unset servers[${#SERVERS [@]}]#Echo "gets the length of the array 2 ="${#SERVERS [@]}Echo '------------------------------before-------------------'# conditionInstall: Case$SERVERinch    #------------------------------------------------------------------------------------# Push Stream distribution Video Server #------------------------------------------------------------------------------------    # [1] Push Flow ######################################################################################"${push_server}")    Sleep 1    ;; #------------------------------------------------------------------------------------# Reverse Proxy proxies #------------------------------------------------------------------------------------    ######################################################################################    #"${servers[3]}"gets the last element of the array as a live node CDN"${proxy_server}")    Sleep 1    ;; *) Log Debug"------------------current IP-------------------"${server} forIinch${servers[@]} Do      Echo "--"$i Done    ;;Esac

Linux command Detailed (10) An array of shell scripts

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.