The definition format of the function:
function Funname ()
{
Action
return xxx;
}
1, can be with function fun () definition, can also be 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 value n
1. function without return statement
Demofun () { echo" This is a shell function "echo" -----Function starts executing----- " Demofun Echo " -----function is finished----- "
2. function with return statement
funcwithresult () { echo " This function adds two numbers to the input ; echo " Enter the first number: " ; Read Anum; echo " Enter a second number: echo " The sum of the two numbers entered is: $? "
Note:
1, function return value after calling this function through $? To obtain;
2, all functions must be defined before use, the calling function only uses the name of its functions;
3. Function parameters
Demofun () {Echo "The first parameter , $" Echo "The second parameter, a" Echo "Tenth parameter $ A" Echo "Tenth parameter ${10}" Echo "The total number of parameters is $#" Echo "output all parameters as a string $*"} demofun1 2 3 4 5 6 7 8 9 the -
Note: $ $ cannot get the tenth parameter, and the tenth parameter requires ${10}. When n>=10, you need to use ${n} to get the parameters
Special 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] same as $*, but quoted when used, 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.
Shell Script Learning Note nine: Shell functions