tech:bash_bonnes_pratiques
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| tech:bash_bonnes_pratiques [2025/05/27 09:32] – Jean-Baptiste | tech:bash_bonnes_pratiques [2026/04/21 10:46] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | Voir : | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * http:// | ||
| + | * http:// | ||
| + | * https:// | ||
| + | * http:// | ||
| + | * http:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * http:// | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | # Bash Les bonnes pratiques | ||
| + | |||
| + | Faire des tests unitaires avec bats | ||
| + | |||
| + | |||
| + | ## Les pièges | ||
| + | |||
| + | ~~~bash | ||
| + | rm -rf $var1/$var2 | ||
| + | ~~~ | ||
| + | |||
| + | Si '' | ||
| + | |||
| + | Solution : nounset | ||
| + | |||
| + | ~~~bash | ||
| + | #! /bin/bash | ||
| + | set -o nounset | ||
| + | ~~~ | ||
| + | |||
| + | et quand nécessaire utiliser | ||
| + | ~~~bash | ||
| + | [[ -z " | ||
| + | ~~~ | ||
| + | |||
| + | Don’t use: | ||
| + | ~~~bash | ||
| + | cd " | ||
| + | [...] | ||
| + | cd .. | ||
| + | ~~~ | ||
| + | but | ||
| + | ~~~bash | ||
| + | ( | ||
| + | cd " | ||
| + | [...] | ||
| + | ) | ||
| + | ~~~ | ||
| + | Voir https:// | ||
| + | |||
| + | |||
| + | ## Les forks bombes | ||
| + | |||
| + | .bashrc | ||
| + | newgrp | ||
| + | |||
| + | Solution [[ulimit]] | ||
| + | |||
| + | Voir | ||
| + | |||
| + | |||
| + | |||
| + | ## Les tubes nommés (pipes) | ||
| + | |||
| + | Source : https:// | ||
| + | |||
| + | FIXME : traduire en Français | ||
| + | |||
| + | How to retain exit status codes through pipes in Bash scripts | ||
| + | |||
| + | Suppose you have a line | ||
| + | |||
| + | ~~~bash | ||
| + | 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: | ||
| + | ~~~bash | ||
| + | set -o pipefail | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Arrêt du script lors de la moindre erreur | ||
| + | |||
| + | Voir http:// | ||
| + | |||
| + | ~~~bash | ||
| + | set -euo pipefail | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | #!/bin/bash | ||
| + | set -euo pipefail | ||
| + | IFS=$' | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## if $? SC2181 | ||
| + | |||
| + | Source : https:// | ||
| + | |||
| + | Code problématique | ||
| + | ~~~bash | ||
| + | make mytarget | ||
| + | |||
| + | if [[ $? -ne 0 ]] | ||
| + | then | ||
| + | echo "Build failed" | ||
| + | fi | ||
| + | ~~~ | ||
| + | |||
| + | Code Correct | ||
| + | ~~~bash | ||
| + | if ! make mytarget; | ||
| + | then | ||
| + | echo "Build failed" | ||
| + | fi | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ## Test | ||
| + | |||
| + | https:// | ||
| + | |||
| + | |||
| + | |||
| + | ## 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 '' | ||
| + | |||
| + | ~~~bash | ||
| + | #mkdir /tmp/plop | ||
| + | install -d /tmp/plop | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | Préférer un '' | ||
