Update 08-12-2019
This commit is contained in:
101
docs/Divers/bash/bash_exemples.md
Normal file
101
docs/Divers/bash/bash_exemples.md
Normal file
@@ -0,0 +1,101 @@
|
||||
# Bash (exemples)
|
||||
|
||||
|
||||
|
||||
Ajouter un préfixe à la ligne d'un fichier texte:
|
||||
|
||||
```bash
|
||||
brew list | sed -e 's/^/brew install /' > brew-list.txt
|
||||
```
|
||||
|
||||
Pour un suffixe:
|
||||
|
||||
```bash
|
||||
sed -e 's/$/suffix/'
|
||||
```
|
||||
|
||||
|
||||
|
||||
Cherche tous les .jpg d'un répertoire et les effacer:
|
||||
|
||||
```bash
|
||||
find . -name '*jpg' -exec rm {} +
|
||||
```
|
||||
|
||||
|
||||
|
||||
Concaténer tous les .csv:
|
||||
|
||||
```bash
|
||||
find . -type f -iname "*.csv" -exec cat {} \; > toutes.txt
|
||||
```
|
||||
|
||||
|
||||
Couleurs dans le terminal:
|
||||
|
||||
```bash
|
||||
export UNDERLINE=$(tput sgr 0 1)
|
||||
export BOLD=$(tput bold)
|
||||
export BLACK=$(tput setaf 0)
|
||||
export RED=$(tput setaf 1)
|
||||
export GREEN=$(tput setaf 2)
|
||||
export YELLOW=$(tput setaf 3)
|
||||
export BLUE=$(tput setaf 4)
|
||||
export PURPLE=$(tput setaf 5)
|
||||
export CYAN=$(tput setaf 6)
|
||||
export WHITE=$(tput setaf 7)
|
||||
export GRAY=$(tput setaf 8)
|
||||
export LIGHTRED=$(tput setaf 9)
|
||||
export LIGHTGREEN=$(tput setaf 10)
|
||||
export LIGHTYELLOW=$(tput setaf 11)
|
||||
export LIGHTBLUE=$(tput setaf 12)
|
||||
export LIGHTPURPLE=$(tput setaf 11)
|
||||
export LIGHTCYAN=$(tput setaf 11)
|
||||
export RESET=$(tput sgr0)
|
||||
```
|
||||
|
||||
```bash
|
||||
echo "${BLUE}blue${RESET} ${RED}red${RESET}"
|
||||
blue red
|
||||
for i in {0..255}; do echo "$(tput setaf $i)test"; done
|
||||
|
||||
```
|
||||
|
||||
Supprimer récursivement tous les fichiers .DS_ST0RE:
|
||||
|
||||
```bash
|
||||
sudo find / -name ".DS_Store" -depth -exec rm {} \;
|
||||
```
|
||||
|
||||
```bash
|
||||
find . -name '*.DS_Store' -type f -delete
|
||||
```
|
||||
|
||||
```bash
|
||||
find . -name '.DS_Store' -type f -print -exec rm -f {} +
|
||||
```
|
||||
|
||||
Commentaire multi-lignes:
|
||||
|
||||
```bash
|
||||
res1=$(gdate +%s.%N)
|
||||
|
||||
: <<'END_COMMENT'
|
||||
brew info oniguruma
|
||||
brew info gnutls
|
||||
brew info php
|
||||
|
||||
res2=$(gdate +%s.%N)
|
||||
dt=$(echo "$res2 - $res1" | bc)
|
||||
dd=$(echo "$dt/86400" | bc)
|
||||
dt2=$(echo "$dt-86400*$dd" | bc)
|
||||
dh=$(echo "$dt2/3600" | bc)
|
||||
dt3=$(echo "$dt2-3600*$dh" | bc)
|
||||
dm=$(echo "$dt3/60" | bc)
|
||||
ds=$(echo "$dt3-60*$dm" | bc -l)
|
||||
|
||||
#printf "\e[1;34mTotal runtime: %d:%02d:%02d:%02.4f\n" $dd $dh $dm $ds
|
||||
echo -e "\e[1;34m $dd $dh $dm $ds \e[0m"
|
||||
END_COMMENT
|
||||
```
|
||||
|
||||
145
docs/Divers/bash/programmation.md
Normal file
145
docs/Divers/bash/programmation.md
Normal file
@@ -0,0 +1,145 @@
|
||||
# Bash
|
||||
|
||||
|
||||
|
||||
#### Variables:
|
||||
|
||||
##### Incrémenter une variable:
|
||||
|
||||
```bash
|
||||
i=$((i+1)) user 0m0.992s
|
||||
i=$((i++)) user 0m0.964s
|
||||
((i=i+1)) user 0m0.760s
|
||||
((i+=1)) user 0m0.700s
|
||||
((i++)) user 0m0.644s
|
||||
((++i)) user 0m0.556s
|
||||
let "i=i+1" user 0m1.116s
|
||||
let "i+=1" user 0m1.100s
|
||||
let "i++" user 0m1.008s
|
||||
let i=i+1 user 0m0.952s
|
||||
let i+=1 user 0m1.040s
|
||||
let i++ user 0m0.820s
|
||||
declare -i i; i=i+1 user 0m0.528s
|
||||
declare -i i; i+=1 user 0m0.492s
|
||||
i=0; i=$(expr $i + 1) user 0m5.464s
|
||||
```
|
||||
|
||||
##### Concaténer une chaine:
|
||||
|
||||
```bash
|
||||
upd+="$name "
|
||||
```
|
||||
|
||||
#### Trim une chaine:
|
||||
|
||||
##### 1. sed
|
||||
|
||||
```bash
|
||||
$ var=" Une chaine entourée d'espaces "
|
||||
|
||||
$ echo "Fluctuat nec $var mergitur"
|
||||
Fluctuat nec Une chaine entourée d'espaces mergitur
|
||||
|
||||
# supprimer les espaces en fin de chaine: sed 's/ *$//g'`
|
||||
$ var=`echo $var | sed 's/ *$//g'`
|
||||
|
||||
$ echo "Fluctuat nec $var mergitur"
|
||||
Fluctuat nec Une chaine entourée d'espaces mergitur
|
||||
|
||||
# supprimer les espaces en début de chaine: sed 's/^ *//g'`
|
||||
$ var=`echo $var | sed 's/^ *//g'`
|
||||
|
||||
$ echo "Fluctuat nec $var mergitur"
|
||||
Fluctuat nec Une chaine entourée d'espaces mergitur
|
||||
|
||||
# supprimer les espaces en début et fin de chaine:
|
||||
|
||||
$ var=" Une chaine entourée d'espaces "
|
||||
$ echo "$var" | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//'
|
||||
Une chaine entourée d'espaces
|
||||
```
|
||||
|
||||
##### 2. awk
|
||||
|
||||
```bash
|
||||
$ echo "${var}"
|
||||
Une chaine entourée d'espaces
|
||||
|
||||
$ echo "${var}" | awk '{gsub(/^[ \t]+/,""); print $0, " fin" }'
|
||||
Une chaine entourée d'espaces fin
|
||||
|
||||
$ echo "${var}" | awk '{gsub(/[ \t]+$/,""); print $0, " fin" }'
|
||||
Une chaine entourée d'espaces fin
|
||||
|
||||
$ echo "${var}" | awk '{gsub(/^[ \t]+| [ \t]+$/,""); print $0, " fin" }'
|
||||
Une chaine entourée d'espaces fin
|
||||
```
|
||||
|
||||
##### 3. xargs
|
||||
|
||||
```bash
|
||||
$ var=" Une chaine entourée d espaces "
|
||||
|
||||
$ echo "$var" | xargs
|
||||
Une chaine entourée d espaces
|
||||
|
||||
$ echo " Une chaine entourée d espaces " | xargs
|
||||
Une chaine entourée d espaces
|
||||
|
||||
# erreur si la chaine contient un apostrophe
|
||||
$ echo " Une chaine entourée d'espaces " | xargs
|
||||
xargs: unterminated quote
|
||||
|
||||
# dans ce cas, on utilise xargs -0
|
||||
$ echo " Une chaine entourée d'espaces " | xargs -0
|
||||
Une chaine entourée d'espaces
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Longueur d'une chaine:
|
||||
|
||||
```bash
|
||||
$ echo -n "Longueur de la chaine:" | wc -c
|
||||
22
|
||||
|
||||
$ echo "Longueur de la chaine:" | awk '{print length}'
|
||||
22
|
||||
|
||||
$ var="Longueur de la chaine:"
|
||||
$ echo ${#var}
|
||||
22
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Déclarer un tableau associatif:
|
||||
|
||||
```bash
|
||||
declare -a do_not_update=('')
|
||||
```
|
||||
|
||||
|
||||
|
||||
##### Test si une commande existe:
|
||||
|
||||
```bash
|
||||
if [ -x "$(command -v jq)" ]; then
|
||||
```
|
||||
|
||||
Tester l'argument d'une commande:
|
||||
|
||||
```bash
|
||||
if [[ $1 == "--nodistract" ]]; then
|
||||
```
|
||||
|
||||
Test du code de retour d'une commande:
|
||||
|
||||
```bash
|
||||
brew missing
|
||||
status=$?
|
||||
if [ $status -ne 0 ]; then
|
||||
```
|
||||
|
||||
179
docs/Divers/bash/tableaux.md
Normal file
179
docs/Divers/bash/tableaux.md
Normal file
@@ -0,0 +1,179 @@
|
||||
# Tableaux en bash
|
||||
|
||||
|
||||
|
||||
#### Création d'un tableau (et initialisation):
|
||||
|
||||
##### Tableau indicé:
|
||||
|
||||
```bash
|
||||
$ tableau_indi=( "un" "deux" "trois" "quatre" )
|
||||
```
|
||||
|
||||
Les indices sont assignés automatiquement, à partir de 0.
|
||||
|
||||
```bash
|
||||
$ tableau_indi=( "chene" "erable" [5]="saule" "hetre"
|
||||
```
|
||||
|
||||
chene à l'indice 0, érable le 2, saule le 5, hetre le 7. Les indices 3 et 4 sont des chaînes vides.
|
||||
|
||||
|
||||
|
||||
##### Tableau associatif:
|
||||
|
||||
```bash
|
||||
$ tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
|
||||
```
|
||||
|
||||
|
||||
|
||||
##### Avec declare:
|
||||
|
||||
```bash
|
||||
$ declare -a tableau_indi
|
||||
$ declare -A tableau_asso
|
||||
```
|
||||
|
||||
Initialisation:
|
||||
|
||||
```bash
|
||||
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
|
||||
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
|
||||
```
|
||||
|
||||
Tableau en lecture seule:
|
||||
|
||||
```bash
|
||||
$ readonly -a tableau_indi_ro=( "un" "deux" "trois" "quatre" )
|
||||
$ readonly -A tableau_asso_ro=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Affichage du tableau:
|
||||
|
||||
L’affichage de l’ensemble d’un tableau se fait avec la syntaxe `${montableau[*]}` ou`${montableau[@]}`
|
||||
|
||||
```bash
|
||||
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
|
||||
$ echo ${tableau_indi[@]}
|
||||
un deux trois quatre
|
||||
|
||||
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
|
||||
$ echo ${tableau_asso[@]}
|
||||
gland faine sirop
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Lecture d'un élément:
|
||||
|
||||
```bash
|
||||
$ tableau_indi=( "un" "deux" "trois" "quatre" )
|
||||
$ echo ${tableau_indi[2]}
|
||||
deux
|
||||
$ echo $tableau_indi
|
||||
un deux trois quatre # !!! normalement 'un'
|
||||
|
||||
|
||||
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
|
||||
$ echo ${tableau_asso["erable"]}
|
||||
# !!!! rien sur zsh !!!!
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Modification d'un élément:
|
||||
|
||||
Si le tableau n’existe pas, il sera créé comme un tableau indicé :
|
||||
|
||||
```bash
|
||||
$ tableau_indi[1]="chene"
|
||||
|
||||
$ echo ${tableau_indi[1]}
|
||||
chene
|
||||
```
|
||||
|
||||
Il n’est pas possible de créer un tableau associatif en lui assignant un élément, il faut le déclarer explicitement avant l’assignation
|
||||
|
||||
|
||||
|
||||
Afficher la liste des clés:
|
||||
|
||||
```bash
|
||||
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
|
||||
|
||||
$ echo ${!tableau_indi[@]}
|
||||
zsh: event not found: tableau_indi[@] # !!!
|
||||
|
||||
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
|
||||
|
||||
$ echo ${!tableau_asso[@]}
|
||||
zsh: event not found: tableau_asso[@] # !!!
|
||||
```
|
||||
|
||||
|
||||
|
||||
Obtenir la taille d'un tableau:
|
||||
|
||||
```bash
|
||||
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
|
||||
$ echo ${#tableau_indi[@]}
|
||||
4
|
||||
|
||||
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
|
||||
$ echo ${#tableau_asso[@]}
|
||||
3
|
||||
```
|
||||
|
||||
|
||||
|
||||
Supprimer un élément:
|
||||
|
||||
```bash
|
||||
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
|
||||
$ echo ${tableau_indi[@]}
|
||||
un deux trois quatre
|
||||
|
||||
$ unset tableau_indi[1]
|
||||
zsh: no matches found: tableau_indi[1]
|
||||
|
||||
$ echo ${!tableau_indi[@]}
|
||||
zsh: event not found: tableau_indi[@]
|
||||
|
||||
$ echo ${tableau_indi[@]}
|
||||
un deux trois quatre
|
||||
```
|
||||
|
||||
```bash
|
||||
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
|
||||
$ echo ${tableau_asso[@]}
|
||||
gland faine sirop
|
||||
|
||||
$ unset tableau_asso['erable']
|
||||
zsh: no matches found: tableau_asso[erable]
|
||||
|
||||
$ echo ${!tableau_asso[@]}
|
||||
zsh: event not found: tableau_asso[@]
|
||||
|
||||
$ echo ${tableau_asso[@]}
|
||||
gland faine sirop
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
Supprimer un tableau entier:
|
||||
|
||||
```bash
|
||||
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
|
||||
|
||||
$ unset tableau_indi
|
||||
# unset tableau_indi[@]
|
||||
# unset tableau_indi[*]
|
||||
|
||||
$ echo ${!tableau_indi[@]}
|
||||
zsh: event not found: tableau_indi[@]
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user