Shell Script Specification format

Source: Internet
Author: User
Tags function definition stop script

#!/bin/bash

#Date: Writing Time 2016-11-22 15:04:12 # #date "+%y-%m-%d%h:%m:%s"

#Author: author

#Mail: Email [email protected]

#Function: Script Introduction

#Version: version V1.1

#Update: Script Modification Update time

6.1.1 version and Operating parameters:

1) before script development, in the beginning of the script in the form of a note SD script version number

2) Add the necessary run parameters for the script, such as:-v to display the version number; -h display Help;

6.1.2 Naming Conventions

Variable name:

1) to unify the variable name, use all caps or hump syntax

2) avoid non-meaning characters or numbers

3) global variables and local variable naming rules

Global Variables Use {}, for example ${jorbabe_home} or "${jorbabe_home}"

4) variable merging: When a variable or configuration needs to be combined to make sense, it is recommended that the merge be assigned to a new variable

5) Variable definition summary, multi-learning simulation system comes with the /etc/init.d/functons function library script definition idea

Function name :

1) The function name is capitalized with the first letter of the word, or you can use hump syntax

2) You can add a prefix

3) When using abbreviations, use the first letter capitalization

4) name try not to use abbreviations unless he is different

5) A name can consist of two or three words, but usually should not be more than three

Script (module) naming:

1) regular shell scripts use the unified suffix: ". Sh" , for example:jorbabe.sh

2) The module's start-stop script is uniformly named start_{ module name }.sh and stop_{ module name }.sh

3) Monitoring script can be *_mom.sh suffix, control script with *_ctl.sh suffix

4) module naming should be representative of its features and functions, do not use the form of a personal initials, etc.

5) script and binary program naming in modules is forbidden and duplicate names of other scripts and binaries

Temporary file naming

try to avoid temporary files, if you must use them, please use The PID does the suffix and clears after the script finishes.

6.1.3 Code Style Specification

Code Framework

1) script start interpreter declared as #!/bin/bash or #!/bin/sh, try to unify

2) references such as configuration files and function scripts, such as source conf/httpd.conf

3) The main script process only implements the program backbone, the function implementation as far as possible encapsulated in the child function.

4) for scripts that can be executed independently , use the usage and version functions, and you can output usages and versioning information.

5) canonical code tree.

function specification

1) The function definition is preceded by a function reserved word,which is not done in/etc/init.d/functions.

2) Display the function return value, display the return statement at the end of the function , and keep the returned value.

Conditional statements and Loops

1) when using conditional statements and loops, try to use a uniform format instead of using "; "Delimited

2) try to line up a statement instead of using "; to separate multiple statements. Make as many judgments as possible about the success of the operation, and do the processing of the response.

3) Use simple statements to avoid multiple-pipe commands.

4) The multi-filter statements for the grep cut, awk , and other commands in the script should be considered for simplification.

6.1.4 Annotation Specification

File Module Comments : Description of the purpose, version input and output files, dependent tools and their version information, before and after the process script (optional), format unified.

Important Function Comment: Description function purpose, parameter, return value, author, version.

Get into the habit of writing notes: Comments to be detailed, including variable definitions, function definitions, return value definitions, each step of the operation purpose, comments as far as possible using standard English.

Tips: comments as little as possible in Chinese comments, in order to avoid changes in the operating environment, Chinese garbled.

do not use a character set that can be defined in a script with export lang= "ZH_CN GB18030" adjusted


Shell Script Specification format

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.