Skip to content

Commit 9dca9e9

Browse files
committed
Minor notes and suggestions for #7
1 parent eb17087 commit 9dca9e9

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

README.md

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
## Some `Bash` coding conventions and good practices.
22

3+
Coding conventions are... just conventions.
4+
It helps to have a little fun with scripting,
5+
not to create new war/bias conversations.
6+
7+
Feel free to break the rules at time you can; it's important
8+
that you will always love what you would have written down...,
9+
because scripts can be too fragile, too hard to maintain,
10+
or so many people hate them...
11+
And it's also important to have a consistent way in your whole script.
12+
313
* [Naming and styles](#naming-and-styles)
414
* [Tabs and Spaces](#tabs-and-spaces)
515
* [Pipe](#pipe)
@@ -14,12 +24,13 @@
1424
* [Set -e](#set--e)
1525
* [Techniques](#techniques)
1626
* [A little tracing](#a-little-tracing)
17-
* [Make your script a library](#make-your-script-a-library)
27+
* [Making your script a library](#making-your-script-a-library)
1828
* [Quick self-doc](#quick-self-doc)
1929
* [No excuse](#no-excuse)
2030
* [Meta programming](#meta-programming)
2131
* [Removing with care](#removing-with-care)
2232
* [Shell or Python/Ruby/etc](#shell-or-pythonrubyetc)
33+
* [Contributions](#contributions)
2334
* [Good lessons](#good-lessons)
2435
* [Resources](#resources)
2536
* [Author. License](#author-license)
@@ -31,6 +42,10 @@
3142
Don't use `(smart-)`tabs. Replace a tab by two spaces.
3243
Do not accept any trailing spaces.
3344

45+
Many editors can't and/or aren't configured to display the differences
46+
between tabs and spaces. Another person editor is just not your editor.
47+
Having spaces does virtually help a strange reader of your script.
48+
3449
### Pipe
3550

3651
There are `inline` pipe and `display` pipe. Unless your pipe is too
@@ -52,7 +67,8 @@ short, please use `display` pipe to make things clear. For example,
5267
done
5368

5469
When using `display` form, put pipe symbol (`|`) at the beginning of
55-
of its statement. Never put `|` at the end of a line.
70+
of its statement. Don't put `|` at the end of a line, because it's the
71+
job of the line end (`EOL`) character and line continuation (`\`).
5672

5773
### Variable names
5874

@@ -201,6 +217,21 @@ This saves you from a lot of headaches and critical bugs.
201217
Because `set -u` can't help when a variable is declared and set to empty
202218
value, don't trust it twice.
203219

220+
It's recommended to emphasize the needs of your variables before your
221+
script actually starts. In the following example, the script just stops
222+
when `SOME_VARIABLE` or `OTHER_VARIABLE` is not defined; these checks
223+
are done just before any execution of the main routine is processed.
224+
225+
```
226+
: a lot of method definitions
227+
228+
set -u
229+
: "${SOME_VARIABLE}"
230+
: "${OTHER_VARIABLE}"
231+
232+
: your main routine
233+
```
234+
204235
#### Set -e
205236

206237
Use `set -e` if your script is being used for your own business.
@@ -319,7 +350,7 @@ is invoked, it will print
319350
LOGGING funcB: This is B
320351
```
321352

322-
### Make your script a library
353+
### Making your script a library
323354

324355
First thing first: Use `function` if possible. Instead of writting
325356
some direct instructions in your script, you have a wrapper for them.
@@ -448,6 +479,35 @@ and rewrite the script in another language, `Ruby/Python/Golang/...`.
448479
Anyway, probably you can't deny to ignore `Bash`:
449480
it's still very popular and many services are woken up by some shell things.
450481
Keep learning some basic things and you will never have to say sorry.
482+
Before thinking of switching to Python/Ruby/Golang, please consider
483+
to write better Bash scripts first ;)
484+
485+
## Contributions
486+
487+
### Variable names for arrays
488+
489+
In #7, Cristofer Fuentes suggests to use special names for arrays.
490+
Personally I don't follow this way, because I always try to avoid
491+
to use Bash array (and/or associative arrays), and in Bash
492+
per-se there are quite a lot of confusion (e.g, `LINENO` is a string,
493+
`FUNCNAME` is array, `BASH_VERSION` is ... another array.)
494+
495+
However, if your script has to use some array, it's also possible to
496+
have special name for them. E.g,
497+
498+
```
499+
declare -A DEPLOYMENTS
500+
DEPLOYMENTS["the1st"]="foo"
501+
DEPLOYMENTS["the2nd"]="bar"
502+
```
503+
504+
As there are two types of arrays, you may need to enforce a better name
505+
506+
```
507+
declare -A MAP_DEPLOYMENTS
508+
```
509+
510+
Well, it's just a reflection of some ;)
451511

452512
## Good lessons
453513

0 commit comments

Comments
 (0)