An array of shells

Source: Internet
Author: User

Definition of an array

What is an array? Students who have studied computer programming languages know that the attributes of an array are a set of identical data types (not including the concept of associative arrays proposed by some programming languages). So what is the definition of arrays in the shell, and we look at two types of data: One is a numeric type, the other is a string type, and although the shell itself is a weak type, it can be so distinguished.

Array of numeric types: A pair of parentheses represents an array, and the elements in the array are separated by a "space".

As an example,

Numeric type:

Arr_number= (1 2 3 4 5);

String type: Similarly, use a pair of parentheses to represent the array, where the elements in the array are enclosed in double brackets or single quotation marks, and also separated by a "space".

arr_string= ("abc" "EDF" "SSS"); or arr_string= (' abc ' EDF ' SSS ');

Operation of the array

We use the numeric type array arr_number= (1 2 3 4 5) as the source array for the relevant explanation: Get the array length, read the value of a subscript, assign a value to a subscript, delete, assign, replace, and traverse. To raise a point of knowledge, we want to get the value of a variable in the shell, starting with the $ character, such as: $a or ${a}


Get array length

echo ${#arr_string [*]} or echo ${#arr_string [@]} is in the middle bracket with a # number in front of it

Gets a value for the underlying

${array name [subscript]}

To assign a value to a subscript:

Here are two questions to ask:

The first question is, what happens if the subscript element already exists?

A: The value of the specified value is modified to be the new.

The second question is, if the specified subscript already exceeds the size of the current array, such as the size of the arr_number above is 5, specify an subscript of 10 or 11 or greater than 5 of any value?

A: The newly assigned value is appended to the end of the array. Such as:

[Email protected] script]# arr_number[19]=9

[[email protected] script]# echo ${arr_number[*]}

1 2 3 4 5 8 8 9


Delete operation

Clear an element: unset arr_number[19]

Clears the entire array: unset arr_number


Shard Access

The Shard Access form is: ${array name [@ or *]: start subscript: End subscript}, note that the value of the end subscript element is not included.

Example: Echo ${arr_number[*]:0:4}


Mode substitution

Form: ${array name [* or @]/Mode/new Value}

Example: Echo ${arr_number[*]/2/98}


Traversal of an array

Array traversal We use the FOR statement to demonstrate:

For V in ${arr_number[@]}; Do

Echo $v;

Done


Conclusion: arrays are used in less ways, and then in the process of writing the script, we hope to use the array well


This article is from "think one or two" blog, please be sure to keep this source http://250919938.blog.51cto.com/962010/1918830

An array of shells

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.