bash_coding_style.txt 4.9 KB

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