User Tools

Site Tools


informatique:linux:programmation_shell

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
informatique:linux:programmation_shell [2021/01/22 15:33] – [Les variables] portée des variables pteuinformatique:linux:programmation_shell [2021/02/02 09:08] – [function] pteu
Line 432: Line 432:
  
 ======Les fonctions====== ======Les fonctions======
 +
 +=====function=====
 +
 +Elles permettent de mutualiser le code afin qu'il soit appeler plusieurs fois sans avoir à le ré-écrire.
 +<code bash>
 +function mafonction() {
 +  echo "exécution de mafonction"
 +}
 +
 +# appelle de la fonction, plus bas dans le code
 +mafonction
 +</code>
 +A l'intérieur d'une fonction on peut utiliser des variables locales en les déclarant ''local VAR=""''. Pour envoyer un code de retour on utilise ''return'' (pas ''exit'' comme dans le script principal, sinon on quitte le script !).
 +
 +La variable bash ''$FUNCNAME'' permet d'afficher le nom de la fonction courante.
  
 =====pipe ("|")===== =====pipe ("|")=====
Line 933: Line 948:
   * [[http://tldp.org/LDP/abs/html/index.html|Advanced Bash-Scripting Guide]]   * [[http://tldp.org/LDP/abs/html/index.html|Advanced Bash-Scripting Guide]]
   * https://www.shellcheck.net : super moulinette en ligne pour analyser un script et recommander des améliorations (exsite aussi en package Linux : ''shellcheck''). A utiliser sans modération !   * https://www.shellcheck.net : super moulinette en ligne pour analyser un script et recommander des améliorations (exsite aussi en package Linux : ''shellcheck''). A utiliser sans modération !
 +  * [[https://google.github.io/styleguide/shellguide.html|Shell Style Guide]] by Google
informatique/linux/programmation_shell.txt · Last modified: 2023/10/02 13:35 by pteu