diff --git a/docs/Divers/bash/bash_exemples.md b/docs/Divers/bash/bash_exemples.md new file mode 100644 index 0000000..1511748 --- /dev/null +++ b/docs/Divers/bash/bash_exemples.md @@ -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 +``` + diff --git a/docs/Divers/bash/programmation.md b/docs/Divers/bash/programmation.md new file mode 100644 index 0000000..bc5d878 --- /dev/null +++ b/docs/Divers/bash/programmation.md @@ -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 +``` + diff --git a/docs/Divers/bash/tableaux.md b/docs/Divers/bash/tableaux.md new file mode 100644 index 0000000..38c699d --- /dev/null +++ b/docs/Divers/bash/tableaux.md @@ -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[@] +``` + diff --git a/mkdocs.yml b/mkdocs.yml index a2ee11b..d943da0 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -40,7 +40,6 @@ nav: - Shells: Linux/shell.md - SSH: Linux/ssh.md - Tail / Head: Linux/tail-head.md - - Variables: Linux/variables.md - Divers: Linux/divers.md - Linux Mint: - Index: Mint/index.md @@ -57,7 +56,6 @@ nav: - Serveur web: Mint/webserver.md - macos: - Index: macos/index.md - - Bash (exemples): macos/bash_exemples.md - chflags: macos/chflags.md - Exécuter un script Bash: macos/executer_shell_script.md - getfileinfo - setfile: macos/getfileinfo_setfile.md @@ -73,10 +71,12 @@ nav: - nvm: macos/node/nvm.md - Python: - Index: macos/python/index.md + - Conda: macos/python/conda.md - Django: macos/python/Django.md - pip: macos/python/pip.md - Python 3: macos/python/python3.md - Environnement virtuel: macos/python/virtuel.md + - Ruby: macos/ruby.md - Sécurité (Gatekeeper): macos/securite.md - ssh: - SSH: macos/ssh/ssh.md @@ -165,6 +165,10 @@ nav: - PHP: solus/php.md - Divers: - Index: Divers/index.md + - bash: + - Exemples: Divers/bash/bash_exemples.md + - Programmation: Divers/bash/programmation.md + - Tableaux: Divers/bash/tableaux.md - batch: - Commades DOS (1): Divers/batch/Commandes_DOS.md - Commades DOS (2): Divers/batch/Commandes_DOS_2.md @@ -208,6 +212,7 @@ nav: - zsh: - Antibody: Divers/zsh/antibody.md - zsh: Divers/zsh/zsh.md + - zsh2: Divers/zsh/zsh2.md - MkDocs: mkdocs.md theme: