Shell Script Programming Overview (III): Functions and arrays

Source: Internet
Author: User
Tags variable scope

First, function: function

1.function Concept

function is a Process-programmed , the purpose is to Code Reuse , is a Modular Programming , Structured Programming Programming Method

function: The code of an independent function as a whole, and a name for it; named code snippet

Note:

1) The code snippet that defines the function is not executed automatically, and is executed at call time, so called function, which can be given in code.

2) any location where the function name appears, is automatically replaced with the function code when the code executes;

2. Syntax

Syntax One:

function Function_name {

... function Body ...

}

Syntax Two:

Function_name () {

... function Body ...

}

3. The life cycle of the function: created each time it is called and terminates on return;

Its status returns the result of the state of the last command running in the function body;

To customize the status return value, you need to use: return

RETURN[0-255]

0: Success

1-255: Failure

4. function return value:

(1) The return value of the execution result of the function:

1) output using the Echo or printf command;

2) The execution result of the command called in the function body;

(2) exit status code for the function:

1) The default depends on the exit status code of the last command executed in the function body;

2) Custom: Return

5. Pass parameters to the function:

$1,$2, ... : a reference to a parameter passed to a function;

$* or [email protected]: Reference all Parameters

$#: The number of arguments passed by reference;

When calling a function, the given argument list is separated by a whitespace character after the function name, for example, TestFunc arg1 arg2 arg3 ...

6. function variable scope

Local variables: The scope is the life cycle of the function, and is automatically destroyed at the end of the function;

Methods for defining local variables: Localvariable=value

Local variable: The scope is the life cycle of the shell process that runs the script, so its scope is the current shell script file;

7. function recursion:

The function calls itself directly or indirectly;

two, array: ARRAY

1. Arrays:

variables are memory spaces that store a single element, and arrays are contiguous memory spaces that store multiple elements;

format : array name [index]

Array name: The entire array has only one name;

Array index: numbering starting from 0;

Reference method:${array_name[index]}

Attention:

1) References Array when only the array name is given, the element that refers to subscript 0 is referenced;

2) Associative Arrays : bash-4 and later versions support custom index formats, not just 0,1,2, ... Number Format

2. declaring an array:

Declare-a NAME: declares an indexed array;

Declare-a NAME: declares an associative array;

3. How elements in an array are assigned

(1) Assign only one element at a time :array_name[index]=value

(2) Assign all elements at once :array_name= ("VAL1" "VAL2" "VAL3" ...)

(3) Assign only specific elements :array_name= ([0]= "VAL1" [3]= "VAL4" ...)

NOTE: Bash supports sparse-format arrays;

(4) Interactive reading assignment: read-a array_name

4. The length of the array (the number of elements in the array):

${#ARRAY_NAME [*]}

${#ARRAY_NAME [@]}

5. refer to all elements in the array:

${array_name[*]}

${array_name[@]}

6. array element slicing: ${array_name[@]:offset:number}

Offset: The number of elements to pass;

Numbers: The number of elements to be taken out, and the amount of all elements after the offset is omitted;

7. Append elements to the non-sparse format array:

array_name[${#ARRAY_NAME [*]}]=

8. Delete an element in the array:

Unset Array[index]

9. Associative arrays:

Declare-a Array_Name

Array_name= ([index_name1]= "value1" [index_name2]= "value2" ...)

This article is from the "Xu Ding blog" blog, make sure to keep this source http://xuding.blog.51cto.com/4890434/1740097

Shell Script Programming Overview (III): Functions and arrays

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.