The Linux shell can be user-defined functions and can be called casually in shell scripts.
Shell functions exist in memory, not hard disk files, so it's fast, and the shell can preprocess functions, so the function starts faster than the script.
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.
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
Case:
A simple function call
# VI function1.sh
#!/bin/bash
Demofun () {
echo "This is my first function"
}
echo "function is starting ..."
Demofun
echo "function is over"
The execution results are:
function is starting ...
This is my first function
function is over
return value
The keyword "return" in the function can be placed anywhere in the body of the function, usually to return certain values, the shell will stop execution after execution to return, return to the main program's call line, return value can only be an integer between 0~256, the return value will be saved to the variable "$?" In
Case:
Returns the number of two inputs and
# VI SH function2.sh
#!/bin/bash
Funreturn () {
echo "The function is used to add the figures together ..."
echo "Enter The first number:"
Read NUM1
echo "Enter The second number:"
Read num2
echo "The number is $num 1 and $num 2"
Return $ (($num 1+ $num 2))
}
Funreurn
echo "The sum of the" is $? "
Execution results
# sh function2.sh
The function is used to add the figures together ...
Enter the first number:
4
Enter the second number:
5
The number is 4 and 5
The sum of the number is 9
Parameter passing
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 ...
Case:
# VI function3.sh
#!/bin/bash
Funparam () {
echo "The first param $"
echo "The second Param"
echo "The Ten Param ${10}"
echo "The sum of the Param $#"
echo "Output the param with string format $*"
}
Funparam 1 3 4 5 6 7 8 9 AK
Execution Result:
# sh function3.sh
The first Param 1
The second param
The ten param AK
The sum of the Param 10
Output the all param with string format 1 3 4 5 6 7 8 9 AK
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.
Additional notes:
| Parameter handling |
Description |
| $ |
is the name of the script itself |
| $# |
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] |
Same as $*, but quoted when used, and returns each parameter in quotation marks |
| $- |
Displays the current options used by the shell, same as the SET command function |
| $? |
Displays the exit status of the last command. 0 means no error, and any other value indicates an error |
Shell script from getting started to complex its eight (function)