Linux Shell Scripting Tutorial Series (15) Introduction to Shell Functions

This article is the (fifteenth) part of the Linux Shell Scripting Series . For more Linux Shell tutorials, please see: Linux Shell Scripting Series Tutorials

After the previous article , functions can divide a complex function into several modules, so that the program structure is clearer and the code repetition rate is higher.

All high-level languages ​​support functions, and the shell is no exception. Today, I will introduce the related usage of functions in Shell.

 

First, the syntax of the Shell function

Because the function is a scripting language, it is executed line by line when it is executed. Therefore, the Shell function must be defined before use.

The definition format of the Shell function is as follows:

[function] funname [()]
{
    command;
    [return int;]
}

 

Description: The function keyword is optional and can be added or not.

The curly brackets are used to decorate the function body, and finally the return value. You can add the [return] keyword to specify the return content of the function. If not, the result of the last command will be used as the return value. return is followed by the numeric value n (0-255).

 

2. Example of using Shell function

Next, we will use examples to learn the use of Shell functions.

Example 1:

#!/bin/bash
#author:LinuxUniversity
# url:www.linuxdaxue.com
#date:2016-06-01

demo(){
            echo "This is my first shell function!"
}
echo "-----function starts executing-----"
demo
echo "-----function completed-----"

 

Description: demo is the definition of a function, and the function body has only one sentence: echo "This is my first shell function!"

Then call the function by the function name.

 

Program output:

-----The function starts to execute-----
This is my first shell function!
-----Function execution completed-----

 

This example does not reflect the use of input parameters of functions. Next, I will introduce the method of passing parameters to functions in Shell.

 

3. Shell function parameter processing

In the shell, you can pass arguments to a function when you call it.

Inside the function body, the value of the parameter is obtained in the form of $n, for example, $1 represents the first parameter, $2 represents the second parameter, and $0 represents the script itself.

 

Shell function parameter processing example

Example 2:

Let's look at another example:

#!/bin/sh
#Author:LinuxUniversity
#url:http://www.linuxdaxue.com
#date:2016-06-01
function fSum()
{
        echo "The input parameter is: "$1,$2
        return $(($1+$2))
}
fI'm 5 7
total=$(fSum 3 2)
echo "Return :"$total,$?

 

This defines a function that calculates the sum of parameters. The function can obtain the input parameter value in the form of $1, $2, and then add the two values ​​in the form of $(($1+$2)) and return the result.

 

When calling, it can also be called in the form of parentheses, and the shell will call the commands inside the single parentheses. Therefore, we can think of functions in the shell as defining a new command, which is a command, so each input parameter is directly separated by spaces.

output:

Input parameters are: 5,7
Return : Input parameters are: 3,2,5

 

Notice:

Note that $10 cannot get the tenth parameter, ${10} is required to get the tenth parameter. When n>=10, you need to use ${n} to get the parameters.

 

special characters used in functions

In addition, there are several special characters used to process parameters:

parameter illustrate
$# The number of arguments passed to the script
$* Displays all parameters passed to the script as a single string
$$ The current process ID number of the script running
$! ID number of the last process running in the background
$@ Same as $*, but used with quotes, returning each argument in quotes.
$- Displays the current options used by the shell, which has the same function as the set command.
$? Displays the exit status of the last command. 0 means no error, any other value means there is an error.

Well, the relevant knowledge about Shell functions will be introduced here first. The functions in Shell are very powerful, so I won't talk about them here.

 

For more Linux Shell tutorials, please see: Linux Shell Scripting Series Tutorials

 

 

Original: Linux Shell Series Tutorial (15) Introduction to Shell Functions

Previous: Linux Shell Scripting Tutorial Series (14) Shell Select Tutorial

Next: Linux Shell Scripting Tutorial Series (16) Shell Input and Output Redirection

This article is transferred from: Linux Shell Scripting Tutorial Series (15) Introduction to Shell Functions

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326628175&siteId=291194637