Outils pour utilisateurs

Outils du site


informatique:bash

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
informatique:bash [2019/09/13 23:09] – [Formatage du texte] alexisinformatique:bash [2022/08/14 18:48] (Version actuelle) – [Bonnes pratiques] alexis
Ligne 1: Ligne 1:
 ====== Bash ====== ====== Bash ======
 <WRAP todo> <WRAP todo>
-Regarder et tester le contenu du lien suivantÇa a l'air intéressant ! +Étudier les liens suivants : 
-[[http://shaarli.guiguishow.info/?gKD9Ow|Quand le gruik découvre set -e et trap ERR GuiGui's Show]] +  - [[https://www.tldp.org/LDP/abs/html/index.html|Advanced Bash-Scripting Guide]] 
- +  [[http://www.cyberciti.biz/faq/bash-shell-script-generating-random-numbers/|Bash Shell Generate Random Numbers]] 
-modifier la section des aliases+  - [[https://zwischenzugs.com/2019/02/27/bash-startup-explained/|Bash Startup Explained – zwischenzugs]] 
 +  - [[http://linux-attitude.fr/post/Bashrc-collaboratif|Bashrc collaboratif - Linux Attitude]] 
 +  - [[http://cheat.sh/bash/:learn|cheat.sh/bash/:learn]] 
 +  - [[https://github.com/dylanaraps/pure-bash-bible|dylanaraps/pure-bash-bible: 📖 A collection of pure bash alternatives to external processes.]] 
 +  - [[http://stackoverflow.com/questions/8789729/zero-padding-in-bash|numbers - Zero Padding In Bash - Stack Overflow]] 
 +  - [[http://edoceo.com/liber/linux-bash-shell|The Bash Shell - Edoceo, Inc.]] 
 +  - [[https://www.linuxjournal.com/content/bash-trap-command|The Bash Trap Command | Linux Journal]] 
 +  - [[http://www.linuxjournal.com/content/validating-ip-address-bash-script|Validating an IP Address in a Bash Script | Linux Journal]] 
 +  - [[https://linuxhint.com/debug_bash_script-2/|How to debug a bash script? – Linux Hint]] 
 +  - [[https://wizardzines.com/comics/bash-errors/|bash errors]] 
 +  - [[https://betterdev.blog/minimal-safe-bash-script-template/|Minimal safe Bash script template | Better Dev]]
 </WRAP> </WRAP>
  
-===== Alias utiles ===== + 
-<code bash> + 
-alias 1.="cd .." + 
-alias 2.="cd ../.." +===== Bonnes pratiques ===== 
-alias 3.="cd ../../.." +Liste de pages recensant les bonnes pratiques à appliquer lors de l'écriture de scripts : 
-alias 4.="cd ../../../..+  * [[http://redsymbol.net/articles/unofficial-bash-strict-mode/|Bash Strict Mode]] 
-</code>+  * [[https://blog.seboss666.info/2020/04/programmation-defensive-en-bash/|Programmation défensive en bash – Le blog de Seboss666]] 
 +  * [[http://shaarli.guiguishow.info/?gKD9Ow|Quand le gruik découvre set -e et trap ERR - GuiGui's Show]] 
 +  * [[https://ashishb.net/all/the-first-two-statements-of-your-bash-script-should-be/|The first two statements of your BASH script should be…]] 
 +  * [[https://mywiki.wooledge.org/BashPitfalls|BashPitfalls - Greg's Wiki]] 
 +  * [[https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425|set -e, -u, -o, -x pipefail explanation · GitHub]] 
 +  * [[https://mywiki.wooledge.org/BashFAQ/035|BashFAQ/035 - Greg's Wiki]] 
 + 
 + 
  
  
Ligne 38: Ligne 56:
 # Version longue # Version longue
 env --ignore-environment <command> env --ignore-environment <command>
 +</code>
 +
 +Dans le cas de //cron//, il est possible d'en extraire son environnement d'exécution et de le ré-utiliser pour le lancement manuel de la commande :
 +<code bash>
 +# Ajouter temporairement cette ligne dans cron pour générer le fichier d'environnement
 +* * * * * root env > ~/cronenv
 +
 +# Lancer la commande en utilisant le contenu du fichier précédemment généré
 +env - $(<~/cronenv) /bin/sh <command>
 </code> </code>
 </WRAP> </WRAP>
Ligne 136: Ligne 163:
 !120:s/old/new/:p !120:s/old/new/:p
 </code> </code>
 +
 +<WRAP info>
 +Pour aller plus loin avec l'historique :
 +  * [[https://www.thegeekstuff.com/2008/08/15-examples-to-master-linux-command-line-history/|15 Examples To Master Linux Command Line History]]
 +  * [[http://www.tux-planet.fr/ajouter-la-date-et-lheure-dans-lhistorique-de-bash/|Ajouter la date et l'heure dans l'historique de bash - Tux-planet]]
 +  * [[https://sanctum.geek.nz/arabesque/better-bash-history/|Better Bash history | Arabesque]]
 +  * [[https://www.digitalocean.com/community/tutorials/how-to-use-bash-history-commands-and-expansions-on-a-linux-vps|How To Use Bash History Commands and Expansions]]
 +  * [[http://blog.macromates.com/2008/working-with-history-in-bash/|TextMate Blog » Working With History in Bash]]
 +</WRAP>
 +
 ===== Manipuler un tableau ===== ===== Manipuler un tableau =====
   * Déclaration   * Déclaration
Ligne 243: Ligne 280:
 ===== Manipuler des nombres ===== ===== Manipuler des nombres =====
  
-  * Additions (([[https://stackoverflow.com/a/6348941|How can I add numbers in a bash script?]]))+  * Additions (([[https://stackoverflow.com/a/6348941|How can I add numbers in a bash script?]])) (([[https://askubuntu.com/questions/385528/how-to-increment-a-variable-in-bash|How to increment a variable in bash? - Ask Ubuntu]]))
 <code bash> <code bash>
 # Addition d'entiers # Addition d'entiers
 echo $((i + 3)) echo $((i + 3))
 +
 +# Incrémentation d'une variable
 +((i++))
  
 # Addition de réels # Addition de réels
Ligne 330: Ligne 370:
 Chaque nombre a une fonction particulière et peut être combiné avec les autres. Cependant, il y a des combinaisons qui n'ont aucun sens. Chaque nombre a une fonction particulière et peut être combiné avec les autres. Cependant, il y a des combinaisons qui n'ont aucun sens.
  
-^ Ajout d'attributs                                   ^^ Suppression d'attributs                                       ^^ +^ Ajout d'attributs                                   |^ Suppression d'attributs                                                                        || 
-^ Code               ^ Effet                           ^ Code                     ^ Effet                               +^ Code               ^ Effet                           ^ Code                     ^ Effet                                                                
-| 1                  | Mettre en gras                  | 21                       | Annuler la mise en gras             +| 1                  | Mettre en gras                  | 0                        | Annuler tous les effets ((''\e[0m'' et ''\e[m'' ont le même effet))  
-| 2                  | Réduire la luminosité           | 22                       | Annuler la réduction de luminosité  +| 2                  | Réduire la luminosité           | 22                       | Annuler la réduction de luminosité                                   
-| 3                  | Mettre en italique              | 23                       | Annuler la mise en italique         +| 3                  | Mettre en italique              | 23                       | Annuler la mise en italique                                          
-| 4                  | Souligner avec un trait simple  | 24                       | Annuler le soulignement simple      +| 4                  | Souligner avec un trait simple  | 24                       | Annuler le soulignement (simple ou double)                           
-| 5                  | Faire clignoter                 | 25                       | Annuler le clignotement             +| 5                  | Faire clignoter                 | 25                       | Annuler le clignotement                                              
-| 7                  | Inverser les couleurs           | 27                       | Annuler l'inversion des couleurs    +| 7                  | Inverser les couleurs           | 27                       | Annuler l'inversion des couleurs                                     
-| 8                  | Cacher                          | 28                       | Annuler le cachement                +| 8                  | Cacher                          | 28                       | Annuler le cachement                                                 
-| 9                  | Barrer                          | 29                       | Annuler le barrement                |+| 9                  | Barrer                          | 29                       | Annuler le barrement                                                 | 
 +| 21                 | Souligner avec un trait double  | 55                       | Annuler le surlignement                                              | 
 +| 53                 | Surligner avec un trait simple  |                          |                                                                      | 
 +^ Couleur du texte                                    |^ Couleur du fond                                                                                || 
 +^ Code               ^ Effet                           ^ Code                     ^ Effet                                                                ^ 
 +| 30                 | Texte noir                      | 40                       | Fond noir                                                            | 
 +| 31                 | Texte rouge                     | 41                       | Fond rouge                                                           | 
 +| 32                 | Texte vert                      | 42                       | Fond vert                                                            | 
 +| 33                 | Texte jaune                     | 43                       | Fond jaune                                                           | 
 +| 34                 | Texte bleu                      | 44                       | Fond bleu                                                            | 
 +| 35                 | Texte violet                    | 45                       | Fond violet                                                          | 
 +| 36                 | Texte cyan                      | 46                       | Fond cyan                                                            | 
 +| 37                 | Texte gris clair                47                       | Fond gris clair                                                      | 
 +| 39                 | Texte par défaut                | 49                       | Fond par défaut                                                      | 
 +| 90                 | Texte gris foncé                | 100                      | Fond gris foncé                                                      | 
 +| 91                 | Texte rouge clair               | 101                      | Fond rouge clair                                                     | 
 +| 92                 | Texte vert clair                | 102                      | Fond vert clair                                                      | 
 +| 93                 | Texte jaune clair               | 103                      | Fond jaune clair                                                     | 
 +| 94                 | Texte bleu clair                | 104                      | Fond bleu clair                                                      | 
 +| 95                 | Texte violet clair              | 105                      | Fond violet clair                                                    | 
 +| 96                 | Texte cyan clair                | 106                      | Fond cyan clair                                                      | 
 +| 97                 | Texte blanc                     | 107                      | Fond blanc                                                           | 
 + 
 +Pour plus d'informations, voir les pages suivantes : 
 +  * [[https://misc.flogisoft.com/bash/tip_colors_and_formatting|bash:tip_colors_and_formatting - FLOZz' MISC]] 
 +  * [[https://web.archive.org/web/20190311094012/http://bitmote.com/index.php?post/2012/11/19/Using-ANSI-Color-Codes-to-Colorize-Your-Bash-Prompt-on-Linux|Using ANSI Color Codes to Colorize Your Bash Prompt on Linux - BitMote]] 
 + 
 +Voici un script qui permet d'afficher l'ensemble des combinaisons décrites plus haut à l'exception des suppressions d'attributs : 
 +<code bash bash-color.sh> 
 +#!/usr/bin/env bash 
 +# Show all the colors of the rainbow, should be run under bash 
 +for STYLE in $(seq 0 9; echo 21 53); do 
 +  for FG in $(seq 30 39; seq 90 97); do 
 +    for BG in $(seq 40 49; seq 100 107); do 
 +      CTRL="${STYLE};${FG};${BG}" 
 +      echo -en "\e[${CTRL}m${CTRL}\e[m " 
 +    done 
 +    echo 
 +  done 
 +  echo 
 +done 
 +# Reset 
 +echo -e "\e[m" 
 +</code>
 ===== Liens externes ===== ===== Liens externes =====
   * [[https://stackoverflow.com/questions/3427872/whats-the-difference-between-and-in-bash|What's the difference between [ and [[ in Bash?]]   * [[https://stackoverflow.com/questions/3427872/whats-the-difference-between-and-in-bash|What's the difference between [ and [[ in Bash?]]
Ligne 346: Ligne 429:
   * [[http://www.bashoneliners.com/|Bash One-Liners]]   * [[http://www.bashoneliners.com/|Bash One-Liners]]
   * [[http://www.faqs.org/docs/bashman/bashref.html|Bash Reference Manual]]   * [[http://www.faqs.org/docs/bashman/bashref.html|Bash Reference Manual]]
-  * [[https://misc.flogisoft.com/bash/tip_colors_and_formatting|Bash tips: Colors and formatting]] 
-  * [[http://bitmote.com/index.php?post/2012/11/19/Using-ANSI-Color-Codes-to-Colorize-Your-Bash-Prompt-on-Linux|Using ANSI Color Codes to Colorize Your Bash Prompt on Linux]] 
   * [[http://www.tldp.org/LDP/abs/html/options.html|Options]]   * [[http://www.tldp.org/LDP/abs/html/options.html|Options]]
   * [[https://wiki.bash-hackers.org/|The Bash Hackers Wiki [Bash Hackers Wiki]]]   * [[https://wiki.bash-hackers.org/|The Bash Hackers Wiki [Bash Hackers Wiki]]]
-  * [[https://www.digitalocean.com/community/tutorials/how-to-use-bash-history-commands-and-expansions-on-a-linux-vps|How To Use Bash History Commands and Expansions]]+  * [[https://devhints.io/bash|Bash scripting cheatsheet]] 
 +  * [[https://shellmagic.xyz/|Shell Magic The Ultimate Bash Cheatsheet!]] 
 + 
  
  
  
informatique/bash.1568441347.txt.gz · Dernière modification : 2019/09/13 23:09 de alexis