Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
kb_it:bash_dev [2022/03/16 22:06] – créée befekb_it:bash_dev [2025/05/21 09:37] (Version actuelle) befe
Ligne 1: Ligne 1:
-===== Bash =====+====== Bash ======
  
-==== Lire les vidéos d'un répertoire en boucle ====+===== Variables ===== 
 + 
 +==== Tester l'existence d'une variable ==== 
 + 
 +<code bash> 
 +[ -z ${var+x} ] && echo 'Var is unset' 
 +</code> 
 + 
 +==== Test l'existance d'un programme ==== 
 + 
 +<code bash> 
 +if ! command -v <the_command> 2>&1 >/dev/null 
 +then 
 +    echo "<the_command> could not be found" 
 +    exit 1 
 +fi 
 +</code> 
 + 
 +===== Mail ===== 
 + 
 +==== Envoyer un mail avec sendmail ==== 
 + 
 +<code bash> 
 +sendmail "adress1@domain.ltd,address2@domain.ltd" -s "smtp.domain.ltd" <<- EndOfMail 
 +    Subject: This is the subject 
 +    From: test@domain.ltd 
 +    Content-Type: text/plain; charset="utf8" 
 + 
 +    Hello world ! 
 +EndOfMail 
 +</code> 
 + 
 +===== Tableaux ===== 
 + 
 +==== Déclaration ==== 
 + 
 +<code bash> 
 +declare -A ARRAY 
 +ARRAY[0]="foo" 
 +ARRAY[1]="bar" 
 +</code> 
 + 
 +==== Boucle sur un tableau ==== 
 + 
 +<code bash> 
 +for item in "${ARRAY[@]}"; do 
 +  [...do something whith $item...] 
 +done 
 +</code> 
 + 
 +===== Divers ===== 
 + 
 +==== Gérer les arguments de commande ==== 
 + 
 +<code bash> 
 +options=$(getopt -o c:hp:s: -l hide-pagination,page-count:,page-size:,sort:,help -- "$@"
 +eval set -- "$options" 
 +while true; do 
 +  case "$1" in 
 +    -c|--page-count) 
 +      PAGE_COUNT=$2 
 +      shift 2 
 +      ;; 
 +    -h|--hide-pagination) 
 +      HIDE_PAGES=1 
 +      shift 
 +      ;; 
 +    -p|--page-size) 
 +      PAGE_SIZE=$2 
 +      shift 2 
 +      ;; 
 +    -s|--sort) 
 +      SORT="$2" 
 +      shift 2 
 +      ;; 
 +    --help) 
 +      show_help 
 +      exit 
 +      ;; 
 +    --) 
 +      shift 
 +      break 
 +      ;; 
 +  esac 
 +done 
 + 
 +REPO="$1" 
 +shift 
 +</code> 
 + 
 +==== Récupérer le chemin du répertoire du script en cours d'exécution ==== 
 + 
 +<code bash> 
 +CD="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 
 +</code> 
 + 
 +===== Lire les vidéos d'un répertoire en boucle =====
  
 <code bash> <code bash>