tech:bash_astuces
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_astuces [2025/09/11 11:19] – Jean-Baptiste | tech:bash_astuces [2026/06/16 15:23] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | # Bash astuces | ||
| + | |||
| + | Voir : | ||
| + | * [15 Linux Bash History Expansion Examples You Should Know](http:// | ||
| + | * https:// | ||
| + | |||
| + | |||
| + | ## Check syntax avec shellcheck | ||
| + | |||
| + | Voir aussi : | ||
| + | * https:// | ||
| + | |||
| + | ~~~bash | ||
| + | shellcheck monscript.sh | ||
| + | ~~~ | ||
| + | |||
| + | Si faux positif | ||
| + | ~~~bash | ||
| + | # shellcheck disable=SC2086 | ||
| + | rsync $RSYNC_OPT " | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ~~~bash | ||
| + | $ shellcheck plop.sh | ||
| + | |||
| + | In mkiso-debian.sh line 56: | ||
| + | source " | ||
| + | ^-- SC1090: Can't follow non-constant source. Use a directive to specify location. | ||
| + | ~~~ | ||
| + | |||
| + | Solution | ||
| + | |||
| + | Ajouter en commentaire `shellcheck source=` | ||
| + | |||
| + | `plop.sh` | ||
| + | ~~~bash | ||
| + | # shellcheck source=vars/ | ||
| + | source " | ||
| + | ~~~ | ||
| + | |||
| + | puis | ||
| + | ~~~bash | ||
| + | shellcheck -x plop.sh | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | `.shellcheckrc` | ||
| + | ~~~bash | ||
| + | external-sources=true | ||
| + | shell=bash | ||
| + | color=always | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Variables | ||
| + | |||
| + | Alternative à **eval** pour les variables | ||
| + | Meta variables | ||
| + | ~~~ | ||
| + | $ a=1 | ||
| + | $ b=2 | ||
| + | $ meta_var=" | ||
| + | $ echo ${!meta_var} | ||
| + | 1 | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | Appel de variable | ||
| + | ~~~ | ||
| + | $ B=2 | ||
| + | $ KEY=B | ||
| + | $ echo ${KEY} | ||
| + | B | ||
| + | $ echo ${!KEY} | ||
| + | 2 | ||
| + | ~~~ | ||
| + | |||
| + | Déclaration / affectation de variables dynamiques | ||
| + | ~~~ | ||
| + | $ declare $KEY=12 | ||
| + | $ echo ${!KEY} | ||
| + | 12 | ||
| + | ~~~ | ||
| + | |||
| + | Ou encore | ||
| + | ~~~bash | ||
| + | K=V | ||
| + | |||
| + | declare -n V2=K | ||
| + | # Ce qui revient à : | ||
| + | # V2=" | ||
| + | |||
| + | $ LETTRE=ALPHA | ||
| + | $ ALPHA=A | ||
| + | $ declare -n PLOP=$LETTRE | ||
| + | $ echo $PLOP | ||
| + | A | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Linter | ||
| + | |||
| + | shfmt | ||
| + | https:// | ||
| + | |||
| + | ~~~bash | ||
| + | ~/ | ||
| + | ~~~ | ||
| + | |||
| + | Auto indent | ||
| + | vim | ||
| + | ~~~ | ||
| + | :set expandtab ts=4 sw=4 ai | ||
| + | :retab | ||
| + | ~~~ | ||
| + | |||
| + | bashate | ||
| + | ~~~bash | ||
| + | pip3 install bashate | ||
| + | |||
| + | bashate file.sh | ||
| + | bashate -i E010,E011 file.sh file2.sh | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Autres | ||
| + | |||
| + | `/ | ||
| + | ~~~bash | ||
| + | / | ||
| + | ~~~ | ||
| + | |||
