Shell script function format. Return value. Passing parameters. Variables. Recursion. Function library

Shell function definition

Definition: write the command sequence together in a format to facilitate repeated use of the command sequence

Shell function format

Format one

function 函数名 {

命令序列

}

Format two

函数名() {

命令序列

}

Shell function return value

return means to exit the function and return a value, the value can be displayed by the $? variable in the script

Usage principle :
take the return value at the end of the function, because the $? variable only returns the exit status code of the last command executed. The
exit status code must be 0~255, and the value beyond that is divided by 256 and the remainder

Insert picture description here

Insert picture description here

Shell function passing parameters

Insert picture description here
Insert picture description here

Shell function variable scope

Functions in Shell scripts are only valid in the current Shell environment
. Variables in Shell scripts are globally effective by default.
Limit variables to functions. Use local commands.

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Recursion

Factorial
Insert picture description here
Insert picture description here
recursive lookup
Insert picture description here
Insert picture description here

Create function library

Put commonly used functions into a separate library script, so that when the script is in use, you can directly call the functions in this library

Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/MQ107/article/details/114659640