Function of shell script programming

Source: Internet
Author: User

The function is actually to give a code a name, each time you use this code can directly use the function name to invoke it.

First, create a function

There are two kinds of formats for creating functions:

Format 1:function function name {

function body

}

Format 2: Function name () {

function body

}

Write function names directly when calling functions.

Second, the use of functions

650) this.width=650; "Style=" border-bottom:0px;border-left:0px;height:auto;vertical-align:middle;border-top:0px; border-right:0px; "title=" 1451877881915477.jpg "alt=" 1.jpg "src=" http://www.178linux.com/ueditor/php/upload/image /20160104/1451877881915477.jpg "/>

In the previous example, a function named test was defined at the beginning and then called directly using the function name.

Third, the return value of the function

1. Execution status return value: The last command state result executed in the function body;

650) this.width=650; "Style=" border-bottom:0px;border-left:0px;height:auto;vertical-align:middle;border-top:0px; border-right:0px; "title=" 1451878220796625.jpg "alt=" 2.jpg "src=" http://www.178linux.com/ueditor/php/upload/image /20160104/1451878220796625.jpg "/>

The function executes successfully and the return value is the default of 0.

2. Custom execution Status return value

You can use the return command to define a status return value that has a range of 0-255 values.

650) this.width=650; "Style=" border-bottom:0px;border-left:0px;height:auto;vertical-align:middle;border-top:0px; border-right:0px; "title=" 1451880709235128.jpg "alt=" 5.jpg "src=" http://www.178linux.com/ueditor/php/upload/image /20160104/1451880709235128.jpg "/>

Iv. functions that accept parameters

The function can also bring in parameters, as shown in the following example:

650) this.width=650; "Style=" border-bottom:0px;border-left:0px;height:auto;vertical-align:middle;border-top:0px; border-right:0px; "title=" 1451888043274715.jpg "alt=" 5.jpg "src=" http://www.178linux.com/ueditor/php/upload/image /20160104/1451888043274715.jpg "/>

Two parameters are referenced in the function, you need to pass the parameters to the past, in the label, when using the function.

650) this.width=650; "Style=" border-bottom:0px;border-left:0px;height:auto;vertical-align:middle;border-top:0px; border-right:0px; "title=" 1451888200350013.jpg "alt=" 6.jpg "src=" http://www.178linux.com/ueditor/php/upload/image /20160104/1451888200350013.jpg "/>

V. Create a function library file

You can save a custom function in a file and call the function you want directly when you need it.

The following is a custom library file:

650) this.width=650; "Style=" border-bottom:0px;border-left:0px;height:auto;vertical-align:middle;border-top:0px; border-right:0px; "title=" 1451889139973661.jpg "alt=" 1.jpg "src=" http://www.178linux.com/ueditor/php/upload/image /20160104/1451889139973661.jpg "/>

Use the functions in this library file in your script:

650) this.width=650; "Style=" border-bottom:0px;border-left:0px;height:auto;vertical-align:middle;border-top:0px; border-right:0px; "title=" 1451889215189359.jpg "alt=" 2.jpg "src=" http://www.178linux.com/ueditor/php/upload/image /20160104/1451889215189359.jpg "/>

Running results of the script:

650) this.width=650; "Style=" border-bottom:0px;border-left:0px;height:auto;vertical-align:middle;border-top:0px; border-right:0px; "title=" 1451889238325861.jpg "alt=" 3.jpg "src=" http://www.178linux.com/ueditor/php/upload/image /20160104/1451889238325861.jpg "/>

You can also use a custom function on the command line, just read the library file when the bash shell starts.

To add a library file to the ~/.BASHRC file:

650) this.width=650; "Style=" border-bottom:0px;border-left:0px;height:auto;vertical-align:middle;border-top:0px; border-right:0px; "title=" 1451889467315974.jpg "alt=" 4.jpg "src=" http://www.178linux.com/ueditor/php/upload/image /20160104/1451889467315974.jpg "/>

To use a custom function directly on the command line:

650) this.width=650; "Style=" border-bottom:0px;border-left:0px;height:auto;vertical-align:middle;border-top:0px; border-right:0px; "title=" 1451889493152704.jpg "alt=" 6.jpg "src=" http://www.178linux.com/ueditor/php/upload/image /20160104/1451889493152704.jpg "/>

Function 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.