Voir :
Faire des tests unitaires avec bats
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}" [...] )
Source : https://blog.g3rt.nl/retain-exit-status-code-through-pipe.html
: 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
Voir http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
#!/bin/bash set -euo pipefail IFS=$'\n\t'
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
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.
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