|
| 1 | +BASH CODING STYLE |
| 2 | + |
| 3 | +------------------------------------------------ |
| 4 | +Contents: |
| 5 | + |
| 6 | + 1. Introduction |
| 7 | + 2. Naming Convention |
| 8 | + 3. Coments |
| 9 | + 4. Coding Styles |
| 10 | + 5. Basic formating |
| 11 | + 6. If, For, and While |
| 12 | + 7. Use of shell builtin commands |
| 13 | + |
| 14 | + |
| 15 | +------------------------------------------------ |
| 16 | + |
| 17 | +1. Introduction |
| 18 | + The main reason for using a consistent set of coding conventions is to |
| 19 | + improve the readability of the source code, allowing core team to |
| 20 | + understand new code more quickly and thoroughly. |
| 21 | + |
| 22 | + |
| 23 | +2. Naming Convention |
| 24 | + The names of files, variables and functions serve as comments of a sort. |
| 25 | + So don’t choose terse names—instead, look for names that give useful |
| 26 | + information about the meaning. Names should be English, like other |
| 27 | + comments. They should be descriptive and correspond or to be appropriate to |
| 28 | + functionality which it implements. Names should not be longer than 30 |
| 29 | + characters. Instead spaces use underscores to separate words in a name. And |
| 30 | + it is always good idea to stick to lower case, exceptions are only global |
| 31 | + or enviroment variables. |
| 32 | + |
| 33 | + iCantReadThis.Shell # Bad naming |
| 34 | + backup_mysql_databases.sh # Good naming |
| 35 | + |
| 36 | + PATH='/bin:/home/user/bin' # Global variable (capitals) |
| 37 | + max_users=0 # Local variable |
| 38 | + |
| 39 | + print_user_password() { # |
| 40 | + echo $password # Function naming example |
| 41 | + } # |
| 42 | + |
| 43 | + |
| 44 | +3. Coments |
| 45 | + The total length of a line (including comment) must not exceed more than 80 |
| 46 | + characters. Every file must be documented with an introductory comment that |
| 47 | + provides shorthand information on the file name and its contents. |
| 48 | + #!/bin/bash |
| 49 | + # info: adding web domain |
| 50 | + |
| 51 | + Consecutive line end comments start in the same column. A blank will always |
| 52 | + follow the introductory character of the comment to simplify the detection |
| 53 | + of the beginning of the word. |
| 54 | + cp foo bar # Copy foo to bar |
| 55 | + rm -f foo # Remove foo |
| 56 | + |
| 57 | + Use an extra '#' above and below the comment in the case of multi-line |
| 58 | + comments: |
| 59 | + # |
| 60 | + # Modify the permissions on bar. We need to set them |
| 61 | + # to root/sys in order to match the package prototype. |
| 62 | + # |
| 63 | + chown root bar |
| 64 | + chgrp sys bar |
| 65 | + |
| 66 | + Each script have 4 logical part Variables, Verifications, Action and Vesta. |
| 67 | + Such parts should be devided by following frames. |
| 68 | + #----------------------------------------------------------# |
| 69 | + # Variable&Function # |
| 70 | + #----------------------------------------------------------# |
| 71 | + |
| 72 | + |
| 73 | +5. Basic Formating |
| 74 | + The indentation of program constructions has to agree with the logic |
| 75 | + nesting depth. The indentation of one step usually is 4 spaces. Do not use |
| 76 | + tabs in your code. You should set your editor to emit spaces when you hit |
| 77 | + the tab key. |
| 78 | + cp foo bar |
| 79 | + cp some_reallllllllly_realllllllllllllly_long_path \ |
| 80 | + to_another_really_long_path |
| 81 | + |
| 82 | + |
| 83 | +6. If, For, and While |
| 84 | + To match Kernighan and Ritchie style, the sh token equivalent to the C "{" |
| 85 | + should appear on the same line, separated by a ";", as in: |
| 86 | + if [ $x = 'something' ]; then |
| 87 | + echo "$x" |
| 88 | + fi |
| 89 | + |
| 90 | + for i in 1 2 3; do |
| 91 | + echo $i |
| 92 | + done |
| 93 | + |
| 94 | + while [ $# -gt 0 ]; do |
| 95 | + echo $1 |
| 96 | + shift |
| 97 | + done |
| 98 | + |
| 99 | + |
| 100 | +7. Use of Shell Builtin Commands |
| 101 | + If possible shell buitins should be preferred to external utilities. Each |
| 102 | + call of test true sed awk etc generates a new process. Used in a loop this |
| 103 | + can extend the execution time considerably. So please do not write: |
| 104 | + if test $# -gt 0; then |
| 105 | + Instead use: |
| 106 | + if [ $# -gt 0 ]; then |
| 107 | + |
| 108 | + In the following example the shell parameter expansion is used to get the |
| 109 | + base name and the directory of a path: |
| 110 | + for pathname in $(find -type f -name "*" -print); do |
| 111 | + basename=${pathname##*/} # replaces basename |
| 112 | + dirname=${pathname%/*} # replaces dirname |
| 113 | + dirlength=${#dirname} # expr length |
| 114 | + done |
| 115 | + |
| 116 | + The proper way to write an infinite loop in the shell is to use the ":" |
| 117 | + built-in, which evaluates to true (exit status 0). This is better than |
| 118 | + using "true", because that is *not* a built-in and thus runs /bin/true. |
| 119 | + while :; do |
| 120 | + echo infinite loop |
| 121 | + done |
| 122 | + |
| 123 | + Do not test for non-/empty strings by comparing to "" or ''. always use |
| 124 | + the test operators -n (non-zero-length string) and -z (zero-length string). |
| 125 | + if [ -z "$foo" ]; then |
| 126 | + echo 'you forgot to set $foo' |
| 127 | + fi |
| 128 | + if [ -n "$BASEDIR" ]; then |
| 129 | + echo "\$BASEDIR is set to $BASEDIR" |
| 130 | + fi |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +------------------------------------------------ |
| 135 | + |
| 136 | + BASH CODING STYLE |
| 137 | + skid@vestacp.com |
| 138 | + 2011.12.28 |
| 139 | + |
| 140 | +------------------------------------------------ |
0 commit comments