Skip to content

Commit 12c7324

Browse files
authored
Merge pull request #8 from icy/mintor_notes_and_fix_sharp_7
Minor notes and suggestions for #7
2 parents eb17087 + 41dc0e9 commit 12c7324

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

README.md

Lines changed: 64 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,14 @@
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)
34+
* [Variable names for arrays](#variable-names-for-arrays)
2335
* [Good lessons](#good-lessons)
2436
* [Resources](#resources)
2537
* [Author. License](#author-license)
@@ -31,6 +43,10 @@
3143
Don't use `(smart-)`tabs. Replace a tab by two spaces.
3244
Do not accept any trailing spaces.
3345

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

3652
There are `inline` pipe and `display` pipe. Unless your pipe is too
@@ -52,7 +68,8 @@ short, please use `display` pipe to make things clear. For example,
5268
done
5369

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

5774
### Variable names
5875

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

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

206238
Use `set -e` if your script is being used for your own business.
@@ -319,7 +351,7 @@ is invoked, it will print
319351
LOGGING funcB: This is B
320352
```
321353

322-
### Make your script a library
354+
### Making your script a library
323355

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

452513
## Good lessons
453514

0 commit comments

Comments
 (0)