Some good habits of shell scripting
1. Naming rules
The script name ends with. SH, and names are as famous as possible. Here are a few of the script naming styles that are available for reference. Through the following fresh and refined style, readers can easily understand the role of the script.
clearlog.sh clearlog.sh clearsql.sh snmp_install.sh monitor.sh
2. Scripting Information
Add vivid information to the script, such as the description of the script function, author, time, version information, etc.
#!/bin/bash #调用语言 # filename:log_receiver.sh# description:log number Write DB # Author:karwai Wong # email: [Email prot ected]# revision:1.1# date:2046-04-06
3. Code format
The code format is uniform and concise, avoiding too much fancy. Indentation format to unify, generally use four spaces as an indentation. It is also a good practice to add the appropriate annotations to the contents of the script. The following code is a small script that I created with the file name newscript.sh, which is the ability to add comment information to the newly created file. This information is information about the script. As soon as you execute this script, you can quickly add comment information to the new script file.
It can be done this way: Bash newscript.sh new script file name
#!/bin/bash # filename:shell scripts standard # Description:make shell scripts standard fastly # author:karwai wong # email: [email protected] # Revision: 1.1 # Date: 2046-04-06 # function:make shell scripts standard for newfile new_file_name=$1 if ! grep -q "#!" ${new_file_Name};then cat>>${new_file_name}<<eof #!/bin/bash # # Filename: # Description: # Author: # email: # revision: # date: eof fi # open text form the 9 lines vim +9 ${new_file_name}
4. Some considerations for scripting
Develop good programming habits, attention to detail, the pursuit of extreme, which is to become a good programmer basic conditions. Here are some of the lessons from the next Shell scripting career, which are some tips on shell scripting, and if something is wrong, please point it out.
1. Debugging functions. I divided the debugging into internal debugging and external debugging. internal debugging: Determine the execution state of the script by echo $ command inside the script. External Debug: bash -n script determine if the script syntax is correct bash -x script debug mode, output the execution information of each step 2. Script Execution 1. It is recommended that the/etc/profile file, or the configuration file of the environment variable that is defined by itself be stressed at the beginning of the execution of the script. such as: source /etc/profile source /opt/sh/appenv.sh 2. In some special circumstances, the command referenced in the shell script may be a bin path of its own definition, which will be reported at the time of execution Command not found, the workaround is to complete the path when the command is executed. export path= "/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/apps/bin/ 3. Condition test When testing the condition, try to use [[]] instead of [] or test, because [[]] functions will be more powerful. 4. With or non- can use && | | ! to replace the simple IF-THEN-ELSE-FI statement. This will make the code look more concise. 5. function function use functions as much as possible, define different functions as functions, next time use, directly referencing the function. 6. Log backup Key operations must have log output, Specifically documenting the success or failure of operations and the point in time of execution
This article is from "to be a superman!" blog, reprint please contact the author!
Some good habits of shell scripting