397 liens privés
Let's start with the punchline. Your bash scripts will be more robust, reliable and maintainable if you start them like this:
#!/bin/bash set -euo pipefail IFS=$'\n\t'
I call this the unofficial bash strict mode. This causes bash to behave in a way that makes many classes of subtle bugs impossible. You'll spend much less time debugging, and also avoid having unexpected complications in production.
Un excellent billet de blog qui explique comment faire des scripts bash qui utilisent le mode "strict": c'est à dire qui crashent à la moindre erreur.
Ne surtout pas manquer la partie "problèmes et solutions" http://redsymbol.net/articles/unofficial-bash-strict-mode/#issues-and-solutions dont il est notamment question de :
- Comment faire exécuter des commandes dont on sait qu'elles sortent avec un code de sortie ≠ 0
- Eviter les variables non définies, et pour cela utiliser des valeurs par défaut: http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
- Opérations de nettoyage avec les
Exit Traps
dont j'ai déja parlé ici https://jeekajoo.eu/links/?_kUIBg
"""
The problem
In bash, running test programs which indicate their pass or fail status via an exit code can be problematic when they are run as part of a pipeline in Linux, Mac OS X or Cygwin since the reported exit code of a pipeline is normally the exit code of the last program in the pipeline. If the test program is not the last program in the pipeline, then its exit code will be hidden by the exit code of the last program.
The solution
bash version 3 introduced an option which changes the exit code behavior of pipelines and reports the exit code of the pipeline as the exit code of the last program to return a non-zero exit code. So long as none of the programs following the test program report a non-zero exit code, the pipeline will report its exit code to be that of the test program. To enable this option, simply execute:
set -o pipefail
in the shell where the test program will execute.
"""
via https://github.com/progrium/bashstyle (Always use set -eo pipefail. Fail fast and be aware of exit codes. Use || true on programs that you intentionally let exit non-zero.)