Shell scripts for Linux learning-------Shell functions

Source: Internet
Author: User

[This is my own study notes, welcome reprint, but please specify the source:http://blog.csdn.net/jesson20121020]

Take a look at the shell's function today.

Shell Functionsfunction Definition

The shell allows a set of command sets or statements to form an available block called a shell function.

function definition Format:

Function name () {Command 1 ...                 } or function name () {...}

Functions can be placed in the same file as a piece of code, or in a separate file containing only functions.

The following script is a separate file that contains only functions.

#!/bin/bash#fun_testfunction Hello () {   echo ' hello,today is ' Date ' "return 1}

Now that the function is defined, how to call the function, let's look at the function call.

function Call

For example, the function defined above is called as follows:

#!/bin/bash#fun_testfunction Hello () {   echo "Hello,today is ' Date '" Return 1}echo "now going the function hello" Helloecho "Back from the function"
As can be seen, in fact, as in other languages, in the main program directly with the function name, you can invoke the defined function, we look at the execution results of the script:

[Email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx fun_test.sh [email protected]:~/develop/worksapce/ shell_workspace$./fun_test.sh now going the function Hellohello,today was February 01, 2015 Sunday 20:22:36 Cstback from the Func tion

parameter Passing

Passing parameters to a function is like using positional variables in a script,,..., $9

Let's change the example above:

#!/bin/bash#fun_testfunction Hello () {   echo "hello,$1 today is ' Date '" Return 1}echo "now going the function Hello" hell o Jessonecho "Back from the function"
Here, using parameters with positional variables, do the following:

[Email protected]:~/develop/worksapce/shell_workspace$./fun_test.sh now going the function Hellohello,jesson today is Sunday, February 01, 2015 20:28:38 Cstback from the function

function File

In the above function definition also mentioned, function can be a separate file, that this file is called a function file, then there is a problem, the above example is in the same file, that is, the function and function calls are in the same main program, then for the function file, how to call it.

Still, let's illustrate with an example, define a function file, modify the last script as a function file, as follows

fun_test.sh

#!/bin/bash#fun_testfunction Hello () {   echo "hello,$1 today is ' Date '" return 1}
Here, we define the function file, we now want to call the function file in another script, how to do it????

funfilecall.sh

#!/bin/bash#funfilecall#source function. Fun_test.shecho "Now going to the function hello" Hello Jessonecho "back from the function"
As you can see, the invocation of the function file requires only two steps to implement, the first step is to declare the function file before calling the function, the method (. function file name), note here that there should be a space between the file name and the function filename. Next, you can directly invoke the function defined in the function file, as in the previous call to the function in the same file. The result of the script execution is as follows:

[Email protected]:~/develop/worksapce/shell_workspace$./funfilecall.sh now going to the function Hellohello,jesson Today is February 01, 2015 Sunday 20:37:55 Cstback from the function

Check load functions and delete functions

To view the load function:

Set

Delete a function

unset

We modify the above script as follows:

#!/bin/bash#funfilecall#source function. Fun_test.shsetunset Helloecho "Now going to the function hello" Hello Jessonecho "back from the function"
The results of the implementation are as follows:


... now going to the function hello./funfilecall.sh: line 8:hello: Command not found back from the function
Yes, in the script there is unset hello, which means to delete hello, that is, do not load, so the execution results will not prompt, hello: command not found.

Also, $? You can get the return value of a command, typically, when the command executes successfully, returns 0, and returns a non-0 integer if the execution is unsuccessful. Similarly to functions, you can get the return value of a function by using $.

Shell scripts for Linux learning-------Shell 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.