The Linux shell can be user-defined functions and can be called casually in shell scripts.
The functions in the shell are defined in the following format:
[Function] funname [()]
{
Action
[Return int;]
}
Description
1, can be with function fun () definition, you can also directly fun () definition, without any parameters.
2, the parameter returns, can display add: return returns, if not added, will run the result as the last command, as the return value. Return followed by a value of N (0-255
The following example defines a function and makes a call:
#!/bin/bash
# Author: Rookie Tutorial
# url:www.runoob.com
Demofun () {
echo "This is my first Shell function!"
}
echo "-----function starts execution-----"
Demofun
echo "-----function completed-----"
Output Result:
-----function starts executing-----
This is my first shell function!
-----function is finished-----
The following defines a function with a return statement:
#!/bin/bash
# Author: Rookie Tutorial
# url:www.runoob.com
Funwithreturn () {
echo "This function adds two numbers to the input ..."
echo "Enter first number:"
Read Anum
echo "Enter a second number:"
Read Anothernum
echo "Two numbers are $aNum and $anotherNum, respectively!"
Return$(($aNum + $anotherNum))#一定是 $ (())
}
Funwithreturn
echo "The sum of the two numbers entered is$?!"
The output resembles the following:
This function adds two numbers to the input ...
Enter the first number:
1
Enter a second number:
2
Two numbers are 1 and 2, respectively!
The sum of the two numbers entered is 3!
The function return value is obtained by using $ after calling the function.
Note: All functions must be defined before they are used. This means that the function must be placed at the beginning of the script until the shell interpreter discovers it for the first time before it can be used. The calling function uses only its name of functions.
function Parameters
In the shell, arguments can be passed to a function when it is called. Inside the function body, the value of the parameter is obtained in the form of a $n, for example, $ $ for the first argument, and $ = for the second argument ...
Examples of functions with parameters:
#!/bin/bash
# Author: Rookie Tutorial
# url:www.runoob.com
Funwithparam () {
echo "The first parameter is $!"
echo "The second parameter is $!"
echo "The tenth parameter is $ A!"
echo "Tenth parameter is ${10}!"
echo "11th parameter is ${11}!"
echo "The total number of parameters is $#!"
echo "Output all parameters as a string $*!"
}
Funwithparam 1 2 3 4 5 6 7 8 9 34 73
Output Result:
The first parameter is 1!
The second parameter is 2!
The tenth parameter is ten!
The tenth parameter is a!
The 11th parameter is 73!
The total number of parameters is 11!
Output all parameters as a string 1 2 3 4 5 6 7 8 9 34 73!
Note that the $ $ cannot get the tenth parameter, and the tenth parameter requires ${10}. When n>=10, you need to use ${n} to get the parameters.
In addition, there are several special characters for handling parameters:
$# the number of arguments passed to the script
$* displays all parameters passed to the script in a single string
$$ the current process ID number for the script to run
$! ID number of the last process running in the background
[Email protected] is the same as $*, but is used with quotation marks and returns each parameter in quotation marks.
$-shows the current options that the shell uses, as is the SET command function.
$? Displays the exit status of the last command. 0 means there is no error, and any other value indicates an error.
14-shell function