1. Function format
- Objective: to turn some opposed codes into functions, provide readability and reusability, and avoid writing the same code repeatedly.
- Function format:
- Function keyword: function can be omitted without any parameters;
- The symbol {indicates the beginning of the function body (a separate line after the function name can be used), and the symbol} indicates the end of the function body;
- Function return value:
- Explicit return: add return, followed by the value n(0~255);
- Implicit return: the running result of the last command;
[function] Function name [(parameter list)] { commands; [return int;] }
2. Function call
- Calling function name: all functions must be pre-defined before use, that is, the function is placed at the beginning of the script file, and only the function name can be used when calling the function.
#! /bin/bash foo () { echo "This is foo" return 0 } foo # Call function foo()
3. Function parameters in shell
- In shell, when you call a function, you can pass parameters to the function. When taking parameters inside a function, the value of the parameter is obtained in the form of a special variable $n. For example, $1 represents the first parameter, $2 represents the second parameter,..., $100 represents the 100th parameter. When a function is called, the parameters are passed in the form of command line parameters.
[cdl@h3c/home/cdl/Test]$cat demo.sh #!/bin/bash :<<comment echo "This is shell How to annotate the whole piece of code in the script,You don't need to use it line by line#Comment“ comment printFunParams() { echo "Current file name: $0" echo "The first parameter in the command line parameters: $1" echo "The second parameter in the command line parameters: $2" echo "All parameters passed to a script or function: $@" echo "All parameters passed to a script or function: $*" echo "The total number of all parameters passed to a script or function: $#" } # Calling a function with parameters printFunParams A B C D [cdl@h3c/home/cdl/Test]$./demo.sh Current file name: ./demo.sh The first parameter in the command line parameters: A The second parameter in the command line parameters: B All parameters passed to a script or function: A B C D All parameters passed to a script or function: A B C D The total number of all parameters passed to a script or function: 4
4.shell programming specification
- The script file name ends with. sh, and try to use UTF-8 file encoding;
- The first line of the script file uses #! / bin/bash without spaces and any options;
- Program header comments: script function, author, modification time, etc;
- The global variable is defined at the beginning of an empty line after the program header comment, in uppercase as much as possible;
- Use ${var} for variable reference, and it is not recommended to use $VaR;
- Single quotation mark (recommended constant), double quotation mark (recommended variable), no quotation mark (recommended number);
- The variables in let and (()) should not be added with $, and the variables in expr should not be added with $;
- Pairs of symbols shall be written at one time to prevent omission, such as: {}, [], "";
- There are spaces at both ends of [] or [[]];
- Command replacement, the ${cmd} format is recommended, and the 'cmd' format is not recommended;
- Script code indentation is not mandatory, or use spaces or TAB keys uniformly;
- Key operations are given an execution result status. If the execution fails, exit n (the execution status result of the previous command is checked with $?);
- Key operations need to be logged to record the success, failure and operation time of the operation;
- You can use & &, |𞓜 instead of simple if then else fi statements;
- If the command is too long, use compliance \ to extend it, except for the command with pipeline;
- Try to use function functions, define different functions as different functions, and call functions directly;
- All file names are named in lowercase. rm -rf is prohibited*
- Separate composite commands (if branch structure and for/while loop), and try to use the following methods:
if [ condition ]; then ...... fi while true; do ...... done for value in {1...10}; do ...... done
5. Debugging of shell script
-
Debug trace (sh -x script file name)
- The preferred method for shell script file debugging. After entering the debugging mode, the shell will execute statements in turn to generate debugging information (with a plus sign means that the statement is executed by the shell, and without a plus sign means that the output is generated by the shell)
[cdl@h3c/home/cdl/Test]$sh -x demo.sh + : + printFunParams A B C D + echo 'Current file name: demo.sh' Current file name: demo.sh + echo 'The first parameter in the command line parameters: A' The first parameter in the command line parameters: A + echo 'The second parameter in the command line parameters: B' The second parameter in the command line parameters: B + echo 'All parameters passed to a script or function: A' B C D All parameters passed to a script or function: A B C D + echo 'All parameters passed to a script or function: A B C D' All parameters passed to a script or function: A B C D + echo 'The total number of all parameters passed to a script or function: 4' The total number of all parameters passed to a script or function: 4
-
Check syntax (sh -n script file name)
- The syntax of the script file is checked, but the script is not executed. If there is a syntax error, the shell will report an error. If there is no error, nothing will be displayed.
[cdl@h3c/home/cdl/Test]$cat demo.sh #!/bin/bash :<<comment echo "This is shell How to annotate the whole piece of code in the script,You don't need to use it line by line#Comment“ comment printFunParams() { echo "Current file name: $0" echo "The first parameter in the command line parameters: $1" echo "The second parameter in the command line parameters: $2" echo "All parameters passed to a script or function: $@" echo "All parameters passed to a script or function: $*" echo "The total number of all parameters passed to a script or function: $#" # Calling a function with parameters printFunParams A B C D [cdl@h3c/home/cdl/Test]$sh -n demo.sh demo.sh: line 22: syntax error: unexpected end of file
-
trap syntax (signal capture)
- The trap command is used to specify the command to be executed after receiving the signal(SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGTERM, etc.). Common usage:
- DEBUG signal tracks and analyzes the changes of related variable values;
- Clean up when the script program is interrupted;
- Ask the user whether to terminate the script;
- The trap command is used to specify the command to be executed after receiving the signal(SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGTERM, etc.). Common usage:
-
tee syntax (pipe copy tee -a file name)
- The tee command is used to read the standard input data and output its contents to a file, that is, to the screen and the specified file. For example: ls -l tee file.txt | less