How shell scripts Customize functions

Source: Internet
Author: User

Functions are often used when writing programs, and general development tools have a rich library of functions. But sometimes you need to customize the function to meet our needs.

In Linux, the same is true for shell scripts, which sometimes use custom functions.

The simplest definition of a function is a set of command sets or statements that form an available block called a function.


1. Define the format of the function:

[SQL]View PlainCopy
    1. function-name () {
    2. Command1
    3. ........
    4. }


Or

[Plain]View PlainCopy
    1. #函数名前面也可以加上function关键字
    2. function Function-name () {
    3. Command1
    4. ........
    5. }


2. Function calls

The following is a script instance of a function:

[HTML]View PlainCopy
    1. #!/bin/bash
    2. function Hello () {#声明函数
    3. echo "Hello!" #函数的主体, Output "hello!"
    4. } #函数结束
    5. Hello #调用函数


3. Parameter passing
passing arguments to a function is like using a variable location in a script $1,$2,$3...$9
The following is an instance of a passing parameter:

[HTML]View PlainCopy
    1. #!/bin/bash
    2. function Hello () {
    3. echo "hello! The first parameter is ' $ '.
    4. }
    5. Hello good

#该脚本执行的结果是: hello! The first parameter is ' good '.


4. function files
Save the file of the function, write a function file with the above example as follows:

[HTML]View PlainCopy
    1. #!/bin/bash
    2. function Hello () {
    3. echo "Hello!"
    4. Return 1
    5. }

The Hellofunction file above is a function file that can be called from another script

[HTML]View PlainCopy
    1. #!/bin/bash
    2. . Hellofunction #调用函数文件, there's a space between the dots and the hellofunction.
    3. Hello #调用函数


5. Loading and deleting
To view loaded functions with Set
Cancel loading with unset function-name
Examples are as follows:

[HTML]View PlainCopy
      1. #!/bin/bash
      2. #hellof
      3. . Hellofunction
      4. unset Hello
      5. Hello #因为已经取消载入, so it goes wrong

How shell scripts Customize 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.