{{tag>Bash Script Console}} Voir : * https://google.github.io/styleguide/shellguide.html * https://github.com/google/styleguide/blob/gh-pages/shellguide.md * http://www.kfirlavi.com/blog/2012/11/14/defensive-bash-programming * http://www.linuxjournal.com/content/working-functions-towers-hanoi * https://github.com/icy/bash-coding-style * http://redsymbol.net/articles/unofficial-bash-strict-mode/ * http://mywiki.wooledge.org/BashFAQ/105 * https://blog.yossarian.net/2020/01/23/Anybody-can-write-good-bash-with-a-little-effort * https://github.com/icy/bash-coding-style?tab=readme-ov-file#deprecation * https://www.reddit.com/r/linuxadmin/comments/9x1ist/what_are_your_conventions_with_bashshell_scripts/ * https://www.linkedin.com/pulse/bash-scripting-conventions-engin-polat * https://gist.github.com/the-glima/39905476c82a8acf9c81b7cfe08e5f03 * https://github.com/chenkaie/junkcode/blob/master/bash/Defensive-BASH-Programming.sh * https://bertvv.github.io/cheat-sheets/Bash.html * https://medium.com/better-programming/defensive-programming-to-create-a-robust-ish-shell-script-d9f21292d08a * https://frippertronics.com/posts/defensive_bash_programming.html * https://blog.seboss666.info/2020/04/programmation-defensive-en-bash/ * https://devhints.io/bash * https://kvz.io/blog/bash-best-practices.html & https://github.com/kvz/bash3boilerplate/ * http://unix.stackexchange.com/questions/8759/best-practice-to-use-in-bash = Bash Les bonnes pratiques Faire des tests unitaires avec bats == Les pièges rm -rf $var1/$var2 Si ''$var1'' et ''$var2'' sont vides on un un jolie Solution : nounset #! /bin/bash set -o nounset et quand nécessaire utiliser [ -z "${foo:-}" ] Don’t use: cd "${foo}" [...] cd .. but ( cd "${foo}" [...] ) Voir https://bertvv.github.io/cheat-sheets/Bash.html == Les forks bombes .bashrc newgrp Solution [[ulimit]] Voir == Les tubes nommés (pipes) Source : https://blog.g3rt.nl/retain-exit-status-code-through-pipe.html FIXME : traduire en Français How to retain exit status codes through pipes in Bash scripts Suppose you have a line mysqldump | pigz in your script. Then the exit status code will be of gzip, rather than mysqldump, while the most likely process to fail here is mysqldump. To fix this, add this at the top of your bash scripts: set -o pipefail == Arrêt du script lors de la moindre erreur Voir http://redsymbol.net/articles/unofficial-bash-strict-mode/ set -euo pipefail #!/bin/bash set -euo pipefail IFS=$'\n\t' == if $? SC2181 Source : https://www.shellcheck.net/wiki/SC2181 Code problématique make mytarget if [ $? -ne 0 ] then echo "Build failed" fi Code Correct if ! make mytarget; then echo "Build failed" fi == Test https://github.com/chriscool/sharness == Conventions de nommage Environment variables or shell variables introduced by the operating system, shell startup scripts, or the shell itself, etc., are usually all in CAPITALS1. To prevent your variables from conflicting with these variables, it is a good practice to use lower_case variable names. == Autres A la place de mkdir, privilégier ''install -d'' #mkdir /tmp/plop install -d /tmp/plop Préférer un ''unlink'' à ''rm'' pour supprimer un lien symbolique