Defensive BASH programming
11–20 of 53 posts
Re: Defensive BASH programming
#12Re: Defensive BASH programming
#13I agree that the function call introduces a better name. The problem is that this name is specific to the author of the script. It replaces a reusable tricky knowledge of the language by a simple knowledge of the author usages.
If we take into account the increased verbosity, increased typing, increased number of lines, there is a clear loss in using these functions.
For me, adding a comment would be far enough and far less annoying.
Re: Defensive BASH programming
#14I think the best "defensive" piece of advice you left out that everyone abuses is never pipe `find` results to `xargs.` One should always do `find ... -print0` to a read-while loop because of filenames with whitespace.
find ... -print0 | xargs -0 ... [eventually with -n1]Re: Defensive BASH programming
#15All these recommendation are excellent (except omitting the necessary obsessive quoting of all variables everywhere (just in case they contain a space). I've been enjoying BATS [0] for my bash testing. [1] https://github.com/sstephenson/bats
Re: Defensive BASH programming
#16I'm not sure if scripting can be consider programming. Excessive scripting means lack of software architecture, no to mention the least efficient way to work with the FS
Whether doing so or not is a good idea is another question entirely..
Re: Defensive BASH programming
#17I think the best "defensive" piece of advice you left out that everyone abuses is never pipe `find` results to `xargs.` One should always do `find ... -print0` to a read-while loop because of filenames with whitespace.
Re: Defensive BASH programming
#18Re: Defensive BASH programming
#19I used to write my various glue-things-together scripts in bash, but this quickly becomes a nightmare as the script grows, due to bash's corner cases, syntax, portability issues etc.
Recently I wrote my massive glue-things-together script with nodejs (since I already use node for many things) and it's much more maintainable and I couldn't be more happy. Node 0.12 has execSync which was the missing piece for making node the proper shell scripting platform.
If you are interested, you may want to check shelljs [1] and my snippets for robust `exec()` and `cd()` in node.
[1] https://github.com/shelljs/shelljs [2] https://gist.github.com/jakub-g/a128174fc135eb773631
Re: Defensive BASH programming
#20Three real defensive bash programming tips are:
- Quote all uses of variables
- set -o nounset
- set -o errexit
And many others can be found in and around http://mywiki.wooledge.org/BashFAQ