Outils pour utilisateurs

Outils du site


tech:bash_astuces

Bash astuces

Check syntax avec shellcheck

Voir aussi :

shellcheck monscript.sh

Si faux positif

# shellcheck disable=SC2086
rsync $RSYNC_OPT "${PART}/" "${PART_TMP}/"
$ shellcheck plop.sh
 
In mkiso-debian.sh line 56:
        source "$FIC_PROPERTIES"
        ^-- SC1090: Can't follow non-constant source. Use a directive to specify location.

Solution

Ajouter en commentaire shellcheck source=

plop.sh

# shellcheck source=vars/vm-deb10.var
source "$FIC_PROPERTIES"

puis

shellcheck -x plop.sh

.shellcheckrc

external-sources=true
shell=bash
color=always

Variables

Alternative à eval pour les variables Meta variables

$ a=1
$ b=2
$ meta_var="a"
$ 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

K=V
 
declare -n V2=K
# Ce qui revient à :
# V2="$K"
 
$ LETTRE=ALPHA
$ ALPHA=A
$ declare -n PLOP=$LETTRE
$ echo $PLOP
A

Linter

shfmt https://github.com/mvdan/sh

~/go/bin/shfmt -i 4 -s -w plop.sh

Auto indent vim

:set expandtab ts=4 sw=4 ai
:retab

bashate

pip3 install bashate
 
bashate file.sh
bashate -i E010,E011 file.sh file2.sh

Autres

/etc/skel/.bash_logout

/usr/bin/clear
tech/bash_astuces.txt · Dernière modification : de Jean-Baptiste

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki