Shell programming (vi)---------arrays and functions

Source: Internet
Author: User
Tags function definition

Definition of the array:
Array name = (element 1 element 2 element N) # A pair of parentheses indicates an array, and the elements of the array are separated by a "space" symbol.
Example: myarray= (1 3 5 7 9) # Note the method defined, parentheses, commas.

Define an array: myarray= (1 2 3 4 5)
Reads an element of an array: Echo ${myarray[subscript value]} # Note the array name must be enclosed in {}, and the subscript value will be numbered starting with 0.
Assignment of array elements: myarray[subscript value] = XXX
Show all elements of an array: Echo ${myarray[*]}
Get the length of the array (number of elements): Echo ${#array [@]} or echo ${#array [*]}
Delete an array element: unset myarray[Subscript value]

----------------------------------------------------------------------------------------------------------

The format of the function definition:

# function can be omitted, note () internal without any parameters, if necessary parameters, directly at the time of the call directly with the parameters

#向函数传递参数就像在一般脚本中使用特殊变量 $1, $2 ... $9

#函数取得所传参数后, it's a good idea to first reset the arguments in the function to save the argument.

Function name ()

{
Command 1
Command 2
. . .
}

Shell programming (vi)---------arrays and functions

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.