Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
documentation:tools:langages:bash [2020/06/23 13:29] – [string replace] shellcheck ltaulelldocumentation:tools:langages:bash [2023/01/13 08:48] (Version actuelle) – supprimée ltaulell
Ligne 1: Ligne 1:
-====== Bash ====== 
- 
-:!: le shell par défaut à la création du compte est ''/bin/bash''. Vous pouvez demander à en changer via [[contact:forms:accueil|les formulaires web]]. 
- 
-<hidden> 
-Environnement Debian 7 de base au PSMN, charger le [[documentation:tools:modules|modulefile Base/psmn]] 
-</hidden> 
-===== Références ===== 
- 
- 
-Le manuel de Bash, en français : 
-  * http://abs.traduc.org/ 
- 
-Nombreux trucs & Astuces : 
-  * https://www.shellcheck.net/ **<- use this** :!: 
-  * https://explainshell.com/ 
-  * http://www.davidpashley.com/articles/writing-robust-shell-scripts/  (**robust scripts**) 
-  * http://www.commentcamarche.net/faq/5444-bash-les-parametres 
-  * http://wiki.bash-hackers.org/commands/classictest 
-  * http://coagul.org/drupal/publication/programmation-en-shell-bash-sous-linux 
-  * http://synapse.wordpress.com/2005/12/28/a-dip-in-the-cup-of-bash-scripting/ 
-  * http://splike.com/wiki/Bash_Scripting_FAQ 
-  * http://www.linuxjournal.com/content/bash-preserving-whitespace-using-set-and-eval 
-  * http://linux.101hacks.com/unix/od-command-examples/  (od) 
-  * http://hacktux.com/bash/script/efficient (builtin) 
-  * http://mywiki.wooledge.org/ArithmeticExpression 
-  * http://redsymbol.net/articles/unofficial-bash-strict-mode/ 
-  * https://sipb.mit.edu/doc/safe-shell/ 
-  * http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html 
-  * http://mywiki.wooledge.org/BashPitfalls 
-  * https://blog.seboss666.info/2020/04/programmation-defensive-en-bash/ 
- 
-===== Ordre de chargement des config de shell ===== 
- 
-  * Bash vs zsh 
- 
-Voir ces schémas (en) :  
- 
-__Expected behavior:__ 
-{{ :faq:shell-startup.png?direct&200 |}} 
- 
-__Reality goes ugly:__ 
-{{ :faq:shell-startup-actual.png?direct&200 |}} 
- 
-Source: http://blog.flowblok.id.au/2013-02/shell-startup-scripts.html 
- 
-(outil de création des diagrammes : http://hg.flowblok.id.au/shell-startup) 
- 
- 
- 
- 
- 
-===== Exemples & Astuces ===== 
- 
-==== ShellCheck ==== 
- 
-This utility is installed on PMSN. **Use it without moderation**. You can also [[https://www.shellcheck.net/|use it online]]. 
-==== Debugging ==== 
- 
-  * Use "strict mode with pipefail" (exit without continuing at **first** error): 
- 
-<code bash> 
-set -euo pipefail 
-</code> 
- 
-  * Trace each command verbosely: 
- 
-<code bash> 
-set -x 
-</code> 
- 
-  * explain code & errors 
- 
-<code bash> 
-shellcheck monscript.sh 
-</code> 
- 
-    * online version: https://www.shellcheck.net/ or https://explainshell.com/ 
-    * online explanation for error code ''SC2154'' (example): https://github.com/koalaman/shellcheck/wiki/SC2154 
- 
-==== string replace ==== 
- 
-  * remplace ''home'' par ''scratch'' depuis ''SGE_O_WORKDIR'', sauve dans ''SCRATCHDIR'' 
- 
-<code bash> 
-SCRATCHDIR=${SGE_O_WORKDIR/"home"/"scratch"} 
-</code> 
- 
-  * nomme ''$OUTFILE'' et ''$LOGFILE'' selon ''$INFILE'' (en changeant l'extension) 
- 
-<code bash> 
-INFILE=$1 
-TMPNAME=$(basename "${INFILE%.*}") 
-OUTFILE="$TMPNAME"-done.hdf5 
-LOGFILE="$TMPNAME".log 
-</code> 
- 
-  * Message d'erreur explicite en cas de variable non-settée 
- 
-<code bash> 
-# ${TMPDIR:?"TMPDIR nonexistent"} 
- 
-result=$(ls "${TMPDIR:?"TMPDIR nonexistent"}") 
-if [[ "$result" == "nonexistent" ]] 
-then 
-    echo "TMPDIR n'a pas été crée !" 
-    exit 1 
-fi 
-</code> 
- 
-==== pattern matching ==== 
- 
-<code bash> 
-#! /bin/bash 
- 
-VARIABLE="Let's test this string!" 
- 
-# This is for regular expressions: 
-if [[ "$VARIABLE" =~ "Let's.*ring" ]] 
-then 
-    echo "matched" 
-else 
-    echo "nope" 
-fi 
- 
-# And here we have Bash Patterns: 
-if [[ "$VARIABLE" == L*ing! ]] 
-then 
-    echo "matched" 
-else 
-    echo "nope" 
-fi 
-</code> 
- 
-<code bash> 
-#! /bin/bash 
- 
-VARIABLE="Let's test this string!" 
- 
-# This is for regular expressions: 
-if [[ "$VARIABLE" =~ "Let's.*ring" ]] 
-then 
-    echo "#1 matched" 
-else 
-    echo "#1 nope" 
-fi 
- 
-if [[ "$VARIABLE" =~ Let\'s.*ring ]] 
-then 
-    echo "#2 matched" 
-else 
-    echo "#2 nope" 
-fi 
-</code> 
- 
-Results: 
-#1 nope 
-#2 matched 
- 
-To get #1 matching remove quotes around RegExpr or set compat31 : ''shopt -s compat31'' 
- 
-<code bash> 
-#!/bin/sh 
- 
-thisString="1 2 3 4 5" 
-searchString="1 2" 
-# if you single quote your input, you could do this 
-# searchString=$1 
- 
-case $thisString in 
-  # match exact string 
-  "$searchString") echo yep, it matches exactly;; 
-   
-  # match start of string 
-  "$searchString"*) echo yep, it matches at the start ;; 
- 
-  # match end of string 
-  *"$searchString") echo yep, it matches at the end ;; 
- 
-  # searchString can be anywhere in thisString 
-  *"$searchString"*) echo yep, it matches in the middle somewhere ;; 
-   
-  *) echo nope ;; 
-esac 
-</code> 
- 
- 
-==== Tableaux associatifs ==== 
- 
-On peut nommer les entrées d'un tableau (l'index). 
- 
-  * astuce : 
- 
-<code bash> 
-tab[wwsi]="whatever/wwsi" 
-tab[wwoz]="whatever/to/wwoz" 
-tab[fip]="http://audio.scdn.arkena.com/11016/fip-midfi128.mp3" 
-</code> 
- 
-Lister le tableau associatif : 
- 
-  * brute : 
-<code bash> 
-for i in ${tab[@]}; do echo $i; done 
-</code> 
- 
-  * astuce (boucle induite) : 
-<code bash> 
-printf "%s\n" "${tab[@]}"; done 
-</code> 
- 
-Simplification de code résultante : 
- 
-Parcours du tableau, connaissant l'index (ici $value). 
- 
-  * Avant : 
-<code bash> 
-tindex=$(for i in ${tab[@]}; do echo $i; done | grep ${value}) 
-for ((i=0; i<${#tab[@]}; i++)) 
-do 
-  if [[ "$tindex" == "{tab[$i]}" ]] 
-  then 
-    data=${tab[$i]} 
-  fi 
-done 
-</code> 
- 
-  * Aprés : 
-<code bash> 
-tindex=$(printf "%s\n" "${tab[@]}" | grep ${value}) 
-data="${tab[$tindex]}" 
-</code> 
  
documentation/tools/langages/bash.1592918968.txt.gz · Dernière modification : 2020/08/25 15:58 (modification externe)