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édenteDernière révisionLes deux révisions suivantes | ||
documentation:tools:langages:bash [2018/01/05 09:29] – [Bash] ltaulell | documentation:tools:langages:bash [2023/01/13 08:48] – [Tableaux associatifs] ltaulell | ||
---|---|---|---|
Ligne 3: | Ligne 3: | ||
:!: le shell par défaut à la création du compte est ''/ | :!: le shell par défaut à la création du compte est ''/ | ||
- | < | ||
- | Environnement Debian 7 de base au PSMN, charger le [[documentation: | ||
- | </ | ||
- | ===== Références ===== | ||
+ | <note important> | ||
- | Le manuel de Bash, en français : | ||
- | * http:// | ||
- | Nombreux trucs & Astuces : | ||
- | * http:// | ||
- | * http:// | ||
- | * http:// | ||
- | * http:// | ||
- | * http:// | ||
- | * http:// | ||
- | * http:// | ||
- | * http:// | ||
- | * http:// | ||
- | * http:// | ||
- | * http:// | ||
- | * https:// | ||
- | * http:// | ||
- | |||
- | ===== Ordre de chargement des config de shell ===== | ||
- | |||
- | * Bash vs zsh | ||
- | |||
- | Voir ces schémas (en) : | ||
- | |||
- | __Expected behavior:__ | ||
- | {{ : | ||
- | |||
- | __Reality goes ugly:__ | ||
- | {{ : | ||
- | |||
- | Source: http:// | ||
- | |||
- | (outil de création des diagrammes : http:// | ||
- | |||
- | (FIXME si quelqu' | ||
- | |||
- | |||
- | ===== Shellshock ===== | ||
- | |||
- | FIXME | ||
- | |||
- | **problème avec modulefiles, | ||
- | |||
- | < | ||
- | /bin/bash: module: line 1: syntax error: unexpected end of file | ||
- | /bin/bash: error importing function definition for `BASH_FUNC_module' | ||
- | </ | ||
- | |||
- | * https:// | ||
- | * http:// | ||
- | * http:// | ||
===== Exemples & Astuces ===== | ===== Exemples & Astuces ===== | ||
- | ==== Debugging ==== | ||
- | |||
- | * Use " | ||
- | |||
- | <code bash> | ||
- | set -euo pipefail | ||
- | </ | ||
- | |||
- | * Trace each command verbosely : | ||
- | |||
- | <code bash> | ||
- | set -x | ||
- | </ | ||
- | |||
- | ==== string replace ==== | ||
- | |||
- | * remplace '' | ||
- | |||
- | <code bash> | ||
- | SCRATCHDIR=${SGE_O_WORKDIR/ | ||
- | </ | ||
- | |||
- | * nomme $OUTFILE et $LOGFILE selon $INFILE (en changeant l' | ||
- | |||
- | <code bash> | ||
- | INFILE=$1 | ||
- | TMPNAME=$(basename ${INFILE%.*}) | ||
- | OUTFILE=" | ||
- | LOGFILE=" | ||
- | </ | ||
- | |||
- | * Message d' | ||
- | |||
- | <code bash> | ||
- | # ${TMPDIR:?" | ||
- | |||
- | result=$(ls ${TMPDIR:?" | ||
- | if [[ " | ||
- | then | ||
- | echo " | ||
- | exit 1 | ||
- | fi | ||
- | </ | ||
- | |||
- | ==== pattern matching ==== | ||
- | |||
- | <code bash> | ||
- | #! /bin/bash | ||
- | |||
- | VARIABLE=" | ||
- | |||
- | # This is for regular expressions: | ||
- | if [[ " | ||
- | then | ||
- | echo " | ||
- | else | ||
- | echo " | ||
- | fi | ||
- | |||
- | # And here we have Bash Patterns: | ||
- | if [[ " | ||
- | then | ||
- | echo " | ||
- | else | ||
- | echo " | ||
- | fi | ||
- | </ | ||
- | |||
- | <code bash> | ||
- | #! /bin/bash | ||
- | |||
- | VARIABLE=" | ||
- | |||
- | # This is for regular expressions: | ||
- | if [[ " | ||
- | then | ||
- | echo "#1 matched" | ||
- | else | ||
- | echo "#1 nope" | ||
- | fi | ||
- | |||
- | if [[ " | ||
- | then | ||
- | echo "#2 matched" | ||
- | else | ||
- | echo "#2 nope" | ||
- | fi | ||
- | </ | ||
- | |||
- | Results: | ||
- | #1 nope | ||
- | #2 matched | ||
- | |||
- | To get #1 matching remove quotes around RegExpr or set compat31 : '' | ||
- | |||
- | <code bash> | ||
- | #!/bin/sh | ||
- | |||
- | thisString=" | ||
- | searchString=" | ||
- | # if you single quote your input, you could do this | ||
- | # searchString=$1 | ||
- | |||
- | case $thisString in | ||
- | # match exact string | ||
- | " | ||
- | | ||
- | # match start of string | ||
- | " | ||
- | |||
- | # match end of string | ||
- | *" | ||
- | |||
- | # searchString can be anywhere in thisString | ||
- | *" | ||
- | | ||
- | *) echo nope ;; | ||
- | esac | ||
- | </ | ||
- | |||
- | |||
- | ==== Tableaux associatifs ==== | ||
- | |||
- | On peut nommer les entrées d'un tableau (l' | ||
- | |||
- | * astuce : | ||
- | |||
- | <code bash> | ||
- | tab[wwsi]=" | ||
- | tab[wwoz]=" | ||
- | tab[fip]=" | ||
- | </ | ||
- | |||
- | Lister le tableau associatif : | ||
- | |||
- | * brute : | ||
- | <code bash> | ||
- | for i in ${tab[@]}; do echo $i; done | ||
- | </ | ||
- | |||
- | * astuce (boucle induite) : | ||
- | <code bash> | ||
- | printf " | ||
- | </ | ||
- | Simplification de code résultante : | ||
- | Parcours du tableau, connaissant l' | ||
- | * Avant : | ||
- | <code bash> | ||
- | tindex=$(for i in ${tab[@]}; do echo $i; done | grep ${value}) | ||
- | for ((i=0; i< | ||
- | do | ||
- | if [[ " | ||
- | then | ||
- | data=${tab[$i]} | ||
- | fi | ||
- | done | ||
- | </ | ||
- | * Aprés : | ||
- | <code bash> | ||
- | tindex=$(printf " | ||
- | data=" | ||
- | </ | ||