First, the Shell function
All scripts so far in this tutorial have been executed from beginning to end. This is good, but you may have noticed that some of the script segments are duplicated.
The shell allows a set of command sets or statements to be formed into a usable block called a shell function.
Functions in the shell are defined in the following format:
Copy Code code as follows:
Function name () {
Command1
Command2
...
CommandN
[Return value]
}
If you prefer, you can precede the function name with a keyword function, depending on the user.
Copy Code code as follows:
function functions name () {
Command1
Command2
...
CommandN
[Return value]
}
function returns a value that shows the increment return statement, and if not, the last command runs as the returned value (typically 0, which returns an error code if execution fails). Return followed by a value (0-255).
A function can be placed in the same file as a piece of code, or it can be placed in a separate file that contains only functions. A function does not have to contain many statements or commands, and it can even contain only one echo statement, depending on the consumer.
The following example defines a function and makes a call:
Copy Code code as follows:
#!/bin/bash
Demofun () {
echo "This is your-a-function!"
}
echo "Function begin ..."
Hello
echo "Function end!"
Output:
Function begin ...
This is your the function!
Function end!
The following defines a function with a return statement:
Copy Code code as follows:
#!/bin/bash
Funwithreturn () {
echo "The ' function is ' to ' the sum of two numbers ..."
Echo-n "Input A:"
Read Anum
Echo-n "Input Another number:"
Read Anothernum
echo "The two numbers are $aNum and $anotherNum!"
Return $ (($aNum + $anotherNum))
}
Funwithreturn
echo "The sum of two numbers is $?!"
The output is similar to the following:
The function is to get the sum of two numbers ...
Input number:25
Input another number:50
The two numbers are and 50!
The sum of two numbers is 75!
function returns the value after calling the function through $? To obtain.
Note: All functions must be defined before they are used. This means that you must place the function at the beginning of the script until the shell interpreter discovers it for the first time. Call functions use only their function names.
Second, the Shell function parameters
In a shell, you can pass arguments to a function when it is called. Inside the function body, the value of the parameter is obtained in the form of $n, for example, the first argument is represented, and the second argument is $ $ ...
Examples of functions with parameters:
Copy Code code as follows:
#!/bin/bash
Funwithparam () {
echo "The value of the ' the ' the ' the ' the ' parameter ' is $!
echo "The value of the second parameter is $!"
echo "The value of the tenth parameter is $!"
echo "The value of the tenth parameter is ${10}!"
echo "The value of the eleventh parameter is ${11}!"
echo "The amount of the parameters is $#!"
echo "The string of the parameters is $*!"
}
Funwithparam 1 2 3 4 5 6 7 8 9 34 73
Output:
The value of the ' the ' is 1!
The value of the second parameter is 2!
The value of the tenth parameter is 10!
The value of the tenth parameter is 34!
The value of the eleventh parameter is 73!
The amount of the parameters is 12!
The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73!
Note that $ cannot get the tenth argument, and getting the tenth argument requires ${10}. When n>=10, you need to use ${n} to get the parameters.
In addition, there are several special characters to handle parameters: