Linux shell scripts and linuxshell scripts

Source: Internet
Author: User

Linux shell scripts and linuxshell scripts

[This article is my own learning notes. You are welcome to repost it, but please note the Source: http://blog.csdn.net/jesson20121020]

Let's take a look at Shell functions today.

Shell Function Definition

Shell allows a group of command sets or statements to form an available block. These blocks are called Shell functions.

Function Definition Format:

Function Name () {command 1 ...... } Or function name (){...... }

Functions can be stored in the same file as a piece of code, or in a separate file that only contains functions.

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

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

Now that the function is defined, how can we call this function? Next, let's look at the function call.

Function call

The preceding functions are 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 you can see, like other languages, you can use the function name directly in the main program to call the defined function. Let's take a look at the execution result of the script:

Jesson @ jesson-K43SV :~ /Develop/worksapce/shell_workspace $ chmod a + rx fun_test.sh jesson @ jesson-K43SV :~ /Develop/worksapce/shell_workspace $./fun_test.sh now going the function helloHello, today is Sunday February 01, 2015 20:22:36 CSTback from the function

Parameter transfer

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

Let's modify the example above:

#!/bin/bash#fun_testfunction hello(){   echo "Hello,$1 today is `date`"return 1}echo "now going the function hello"hello jessonecho "back from the function"
Here, the parameters are used as location variables, and the execution is as follows:

Jesson @ jesson-K43SV :~ /Develop/worksapce/shell_workspace $./fun_test.sh now going the function helloHello, jesson today is Sunday, January 1, February 01, 2015 20:28:38 CSTback from the function

Function File

As mentioned in the above function definition, a function can be used as a single file. This file is called a function file, so there is a problem. In the above example, they are all in the same file, that is, the function and function call are all in the same main program. How can we call the function file.

Let's take an example to illustrate how to define a function file and modify the last script as a function file, as shown below:

Fun_test.sh

#!/bin/bash#fun_testfunction hello(){   echo "Hello,$1 today is `date`"return 1}
Here, we have defined the function file. How can we call this function file in another script ????

Funfilecall. sh

 #!/bin/bash#funfilecall#Source function. fun_test.shecho "now going to the function hello"hello jessonecho "back from the function"
We can see that the function file can be called in only two steps. The first step is to declare the function file and method (. function file name). Note that ,. there must be a space between the function file name and function file name. Next, you can directly call the function defined in the function file, just like calling the function in the same file. The script execution result is as follows:

Jesson @ jesson-K43SV :~ /Develop/worksapce/shell_workspace $./funfilecall. sh now going to the function helloHello, jesson today is Sunday, January 1, February 01, 2015 20:37:55 CSTback from the function

Check loading and deleting Functions

View the load function:

Set

Delete A Function

Unset

The above script is modified as follows:

#!/bin/bash#funfilecall#Source function. fun_test.shsetunset helloecho "now going to the function hello"hello jessonecho "back from the function"
The execution result is as follows:


... Now going to the function hello./funfilecall. sh: Line 8: hello: the command back from the function is not found
Yes, there is unset hello in the script. This sentence means to delete "hello", that is, not load it. Therefore, the prompt "hello: no command is found in the execution result.

In addition, $? You can obtain the return value of a command. Generally, if the command is successfully executed, 0 is returned. If the command fails to be executed, a non-zero integer is returned. Similar to functions, you can use $? Obtain the return value of the function.

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.