Array of Shell programming

Source: Internet
Author: User
Tags what array xsl

Array

What array?

An array is a collection that stores multiple storage units together continuously. In Linux, every storage unit in an array is called an array element. By default, the variable type of each array element is character. Therefore, different types of data can also be placed in the same array. The array has an orderly nature.

Definition of an array

Declare-a Array_Name

Array_Name an array name

assigning values to arrays

Assignment method One:

Array_name[index]=value

Index is subscript indexed, starting from 0 by default

For example:

Declare-a XSL

xsl[0]=10

Assignment Method Two:

Array_name= (VALUE1 VALUE2 VALUE3 ...)

Separated by a space before multiple values

Their subscript index starts at 0 and increments backwards

For example:

Xsl= (1991 "Hello Baby" Nice)

If there are spaces in the string, use double quotation marks to

Assignment Method Three:

Array_name= ([0]=value1 [1]=value2 [5]=value5]

With this assignment method, the default value is NULL

Displays the value of an element in an array

${array[index]}

Array name, index subscript subscript

Count the number of elements in an array

${#ARRAY [*]} and ${#ARRAY [@]} can count the number of non-empty elements in the array

${#ARRAY [INDEX]} to display the character length of the specified element

${#ARRAY} and ${#ARRAY [0]} can display the character length of the first element

Example one: Define an array of xsl= ([0]= ' Hello Nice ' [1]=11 [5]=window]

1. Display the value of the first element and the second element

2. Display the character length of the first element, the second element, and the third element

3. Displays the number of non-empty elements of this array

[[email protected] ~]# xsl= ([0]= ' Hello Nice ' [1]=11 [5]=window]

# # #要求1 # #
[[email protected] ~]# echo ${xsl[0]}
Hello Nice
[[email protected] ~]# echo ${xsl[1]}
11

# # #要求2 # #

[[email protected] ~]# echo ${#xsl [0]}
10
[[email protected] ~]# echo ${#xsl [1]}
2

[[email protected] ~]# echo ${#xsl [2]}
0

# # #要求3 # #

[[email protected] ~]# echo ${#xsl [*]}
3
[[email protected] ~]# echo ${#xsl [@]}
3

Example two: Randomly generate 10 random numbers and save them in the array sz, and find out the maximum value of this array

[email protected] ~]# cat shuzhumax.sh
#!/bin/bash
Declare-a sz
For I in {0..9};d o
sz[$I]= $RANDOM
Echo-n "${sz[$I]}"
Done
Echo
Max=${sz[0]}
For I in ' SEQ 1 9 ';d o
If [$MAX-lt ${sz[$I]}]; then
max=${sz[$I]}
Fi
Done
Echo $MAX
[Email protected] ~]# bash shuzhumax.sh
18900 16884 4645 11709 7683 8855 12373 22226 26131 28261
28261
[Email protected] ~]#

This article is from the "Linux Learning path" blog, so be sure to keep this source http://xslwahaha.blog.51cto.com/4738972/1573883

Array of Shell programming

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.