Array of shell script programming

Source: Internet
Author: User

An array is a set of elements of the same data type arranged in a certain order, that is, a variable of the same type is named with a name, and then the set of the variable is distinguished by a number, which is called the array name, and the number is called the subscript. The individual variables that make up the array are called elements of the array. An array is a form in which a number of variables of the same type are organized in an orderly manner in order to deal with them conveniently.

I. How arrays are represented

1. Subscript array

Subscript must be an integer, format: array name [subscript], subscript default starting from 0.

The shell supports sparse arrays, and subscript integers do not need to be contiguous.

Declaration method: Declare-a Array Name

2. Associative arrays

Associative arrays can use arbitrary strings as subscripts (not necessarily integers) to access array elements. Bash4.0 begins to support associative arrays.

Format: array name [arbitrary string]

Declaration method: Declare-a Associative array

In addition, the shell supports only one-dimensional arrays and does not support multidimensional arrays.

Second, the assignment of the array

1. Assign a value to one element at a time

650) this.width=650; "title=" 1451973279841551.jpg "alt=" 1.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160105/1451973279841551.jpg "/>

2. Assign values to all elements at once

650) this.width=650; "title=" 1451973381705205.jpg "alt=" 2.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160105/1451973381705205.jpg "/>

3, by index to assign value

650) this.width=650; "title=" 1451973558986677.jpg "alt=" 3.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160105/1451973558986677.jpg "/>

4. Command substitution Assignment

650) this.width=650; "title=" 1451973856838478.jpg "alt=" 5.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160105/1451973856838478.jpg "/>

5. User input

Using the read command to implement an interactive assignment, format: read-a array name

650) this.width=650; "title=" 1451974079889122.jpg "alt=" 6.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160105/1451974079889122.jpg "/>

Iii. Access to arrays

Access to the array elements is implemented by manipulating the syntax.

There are several syntax formats:

The length of the array:

${#ARRAY [*]}: Displays the number of elements;

${#ARRAY [@]}: Displays the number of elements;

${#ARRAY [#]}: Number of characters of the # element;

${#ARRAY}: The number of characters in the No. 0 element;

Elements of the array:

${array[*]}: Displays the contents of all elements;

${array[@]}: Displays the contents of all elements;

${array[#]}: Displays the first # elements;

${array}: Displays the No. 0 element;

The key value of the array:

$[! Array[*]}: Displays all the key values;

$[! array[@]}: Displays all the key values.

Example: Write a script, generate 10 random numbers, save to an array, and then display an array of elements labeled even:

650) this.width=650; "title=" 1451975351673900.jpg "alt=" 7.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160105/1451975351673900.jpg "/>

Iv. selecting elements from an array

If you want to remove a few elements from an array, it is cumbersome to use the subscript to find each other, and you can use the Offset element feature of the array to simplify the operation.

Syntax format:

${array[@]:offset:number}

Offset: The number of displaced elements;

Number: How many elements are removed;

Example: There are 6 elements in the array off_array, assuming we want to take March to May of these three values:

650) this.width=650; "title=" 1451975899103523.jpg "alt=" 8.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160105/1451975899103523.jpg "/>

650) this.width=650; "title=" 1451976018438229.jpg "alt=" 8.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160105/1451976018438229.jpg "/>

If you want to remove all elements after the offset, omit the nubmer, such as:

650) this.width=650; "title=" 1451976120363326.jpg "alt=" 9.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160105/1451976120363326.jpg "/>

V. Array replication

[Email protected]: Each parameter is a separate string, recommended to use;

$*: All elements as a string.

Example: Copying an even-numbered element in an array to a new array:

650) this.width=650; "title=" 1451977161249991.jpg "alt=" 11.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160105/1451977161249991.jpg "/>

Vi. removing elements from an array

Format: unset array name [subscript]

650) this.width=650; "title=" 1451977271547415.jpg "alt=" 8.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160105/1451977271547415.jpg "/>

Vii. Comprehensive Examples

Generates 10 random numbers and sorts the 10 random numbers.

650) this.width=650; "title=" 1451980426694742.jpg "alt=" 1.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160105/1451980426694742.jpg "/>

650) this.width=650; "title=" 1451980450341045.jpg "alt=" 2.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20160105/1451980450341045.jpg "/>



Array of shell script 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.