| 1 |
This is the rc-scripts style manual. It governs the coding style |
| 2 |
of rc-scripts. Everything here might as well have been spoken by |
| 3 |
God. If you find any issues, please talk to base-system@gentoo.org |
| 4 |
or #gentoo-base on irc.freenode.net. |
| 5 |
|
| 6 |
############# |
| 7 |
# VARIABLES # |
| 8 |
############# |
| 9 |
- User Variables - |
| 10 |
Variables must always be enclosed by {} |
| 11 |
e.g. ${foo} ${bar} |
| 12 |
- Internal Bash Variables - |
| 13 |
Do not use {} with internal variables unless appropriate |
| 14 |
e.g. case $1 in |
| 15 |
e.g. foo=$IFS |
| 16 |
e.g. echo "blah${1}123" |
| 17 |
- Assigning with Quotes - |
| 18 |
When assigning to a variable from another variable or |
| 19 |
a subshell, you do not need quotes, bash handles it |
| 20 |
e.g. foo=${bar} |
| 21 |
e.g. foo=$(uname -a) |
| 22 |
|
| 23 |
######### |
| 24 |
# TESTS # |
| 25 |
######### |
| 26 |
- Brackets - |
| 27 |
Always use the [[ ... ]] form instead of [ ... ] |
| 28 |
- Equality - |
| 29 |
Always use == form instead of = when testing equality. Makes |
| 30 |
for saner looking code (we all love C, don't lie). |
| 31 |
- Quoting - |
| 32 |
When dealing with strings, do not quote the LHS variable. |
| 33 |
The [[ ... ]] handles spaces properly in variables. You |
| 34 |
should always quote strings though. |
| 35 |
e.g. [[ ${foo} = "bar" ]] |
| 36 |
e.g. [[ "foo" != "bar" ]] |
| 37 |
The RHS variable needs to be quoted to prevent against accidental |
| 38 |
pattern matching (unless you want to match, of course). |
| 39 |
e.g. [[ ${foo} = "${bar}" ]] |
| 40 |
If foo=a-b and bar=a-* and ${bar} was not quoted, you would |
| 41 |
get a match! |
| 42 |
|
| 43 |
############### |
| 44 |
# CODE BLOCKS # |
| 45 |
############### |
| 46 |
- Structure - |
| 47 |
Use the more compact form |
| 48 |
e.g. if ... ; then |
| 49 |
e.g. while ... ; do |
| 50 |
Do not use the older form |
| 51 |
e.g. if ... |
| 52 |
then |
| 53 |
- Functions - |
| 54 |
Use the more compact form |
| 55 |
e.g. foo() { |
| 56 |
Do not lead with 'function ' |
| 57 |
e.g. function foo() { |
| 58 |
|
| 59 |
############ |
| 60 |
# COMMENTS # |
| 61 |
############ |
| 62 |
- General - |
| 63 |
Try to include a comment block before sections |
| 64 |
of code to explain what you're attempting |