Slightly off-topic but maybe some people will find it useful: I 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. No…
Defensive BASH programming
21–30 of 53 posts
Re: Defensive BASH programming
#22Re: Defensive BASH programming
#23 main() {
local files=$(ls /tmp | grep pid | grep -v daemon)
}Re: Defensive BASH programming
#24The only thing in this post that can be accurately called defensive is the use of "local" and "readonly". The rest is all just style preferences, which are rather subjective, and none of which are very appealing to me. Three 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
Using pipefail, if any program in a pipeline fails (i.e. exit code != 0), then the exit code for the pipeline will be != 0.
E.g. pipefail can be useful to ensure `curl does-not-exist-aaaaaaa.com | wc -c` doesn't exit with exit code 0..!
Re: Defensive BASH programming
#25I 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.
avoid read-while loop... there are problems with filenames starting/ending with spaces... better use find ... -print0 | xargs -0 ... [eventually with -n1]
Re: Defensive BASH programming
#26Slightly off-topic but maybe some people will find it useful: I 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. No…
sudo -u 2>&1 >> logfile | tee | echoRe: Defensive BASH programming
#27The only thing in this post that can be accurately called defensive is the use of "local" and "readonly". The rest is all just style preferences, which are rather subjective, and none of which are very appealing to me. Three 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
While not as common as nounset and errexit, pipefail is a useful option as well (set -o pipefail). Using pipefail, if any program in a pipeline fails (i.e. exit code != 0), then the exit code for the pipeline will be != 0. E.g. pipefail can be useful to ensure `curl does-not-exist-aaaaaaa.com | wc -c` doesn't exit with exit code 0..!
set -o nounset -o pipefail -o errexitRe: Defensive BASH programming
#28 ls $dir \
| grep something
is the same as ls $dir |
grep somethingRe: Defensive BASH programming
#29 set -u
set -e
with trap 'echo $0 internal error at $LINENO' ERR