Outils pour utilisateurs

Outils du site


tech:notes_script_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
tech:notes_script_bash [2026/04/25 10:08] Jean-Baptistetech:notes_script_bash [2026/06/07 19:12] (Version actuelle) – modification externe 127.0.0.1
Ligne 1: Ligne 1:
 +<!DOCTYPE markdown>
 +{{tag>Script Bash}}
 +
 +# Notes script bash
 +
 +Voir 
 +* https://linux.goffinet.org/administration/scripts-shell/
 +* http://pteu.fr/doku.php?id=informatique:linux:programmation_shell
 +* http://aral.iut-rodez.fr/fr/sanchis/enseignement/IntroProgBash_2022-06-03.pdf
 +* https://www.mode83.net/atelier/centre_ressources/crs_fichiers/Formation/OSR2K9/Linux/Programmation%20Bash.pdf
 +* https://dev.to/rpalo/bash-brackets-quick-reference-4eh6
 +
 +
 +Voir aussi :
 +* tinyramfs (implémentation initramfs écrite en shell POSIX
 +
 +
 +## Variables
 +
 +### Fichier dans une variable - variable heredoc
 +
 +~~~bash
 +ETCHOSTS=$(cat << 'EOF'
 +10.245.97.221      node1
 +10.245.102.221     node1b
 +EOF
 +)
 +~~~
 +
 +## Les boucles
 +
 +Voir aussi :
 +* [[Notes parallel multithread multicore process shell|Les commandes Xargs et Find]] qui peuvent être des alternatives aux boucles
 +
 +
 +### For
 +
 +~~~bash
 +for (( i=1; $i<=10; i=i+1 ))
 +do
 +    echo $i
 +done
 +~~~
 +
 +Voir exemple avec `seq` ci-dessous
 +
 +
 +### seq
 +
 +~~~bash
 +#for i in $(seq 10)
 +for i in $(seq 1 10)
 +do
 +  echo $i
 +done
 +~~~
 +
 +~~~bash
 +for i in {1..5}
 +do
 +  echo $i
 +done
 +~~~
 +
 +~~~bash
 +seq -f "%f" 3 0.8. 6
 +~~~
 +
 +~~~bash
 +seq -f "%g/04/2018" 10
 +~~~
 +
 +~~~bash
 +seq -s - 8
 +~~~
 +
 +
 +### pb curl break
 +
 +`fic.lst`
 +~~~
 +foo
 +bar
 +~~~
 +
 +`plop_sleep.sh`
 +~~~bash
 +#! /bin/bash
 +
 +while read -r var
 +do
 +        echo $var
 +        timeout 1 sleep inf
 +done < fic.lst
 +~~~
 +
 +`plop_curl.sh`
 +~~~bash
 +#! /bin/bash
 +
 +while read -r var
 +do
 +        echo $var
 +        timeout 1 curl -s telnet://localhost:22
 +done < fic.lst
 +~~~
 +
 +~~~
 +$ ./plop_sleep.sh
 +foo
 +bar
 +
 +$ ./plop_curl.sh
 +foo
 +~~~
 +
 +Contournement
 +
 +`plop_curl_2.sh`
 +~~~bash
 +#! /bin/bash
 +
 +while read -r var
 +do
 +        echo $var
 +        echo timeout 1 curl -s telnet://localhost:22 | bash -s --
 +done < fic.lst
 +~~~
 +
 +Vraie solution
 +
 +`plop_curl_2.sh`
 +~~~bash
 +#! /bin/bash
 +
 +while read -r var
 +do
 +        echo $var
 +        timeout 1 curl -s telnet://localhost:22 </dev/null
 +done < fic.lst
 +~~~
 +
 +
 +### Gérer les locks
 +
 +~~~bash
 +flock -n /tmp/plop.lock -c /opt/plop1.sh -c /opt/plop2.sh
 +~~~
 +
 +
 +## bash Options
 +
 +### Bash suid binary privilege escalation
 +
 +~~~bash
 +sudo cp -p /usr/bin/bash /usr/bin/bash-backdoor
 +sudo chmod u+s /usr/bin/bash-backdoor
 +/usr/bin/bash-backdoor -p
 +~~~
 +
  

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki