Shell function application

1. Definition of Shell function

1 Write the command sequence format together to facilitate repeated use of the command sequence.

2 Function return value:

  • return means to exit the function and return an exit value, which can be displayed by the $? variable in the script.

  • Usage principles:
    1. Take the return value as soon as the function ends, because the $? variable only returns the exit status code of the last command executed.
    2. The exit status code must be 0~255, and the value will be divided by 256 if it exceeds .

  • Note: The returned value 0-255 uses $? carried out.

  • Format 1:
    function function name { command sequence }


    Insert picture description here
    Insert picture description here

  • Format 2:
    function name () { command sequence }


    Insert picture description here
    Insert picture description here

2. Function parameter transfer

  • The parameters of the position variable are passed to the script.
    Insert picture description here
    Insert picture description here

3. The scope of function variables

  • Functions in Shell scripts are only valid in the current Shell environment
  • Variables in Shell scripts are globally effective by default
  • Use the local command to limit the variable to the function
    Insert picture description here
    Insert picture description here

Four. Recursion

  • The function calls its own function.
    Insert picture description here
    Insert picture description here

Five. Create a library

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

Guess you like

Origin blog.csdn.net/LI_MINGXUAN/article/details/111866866