Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
informatique:outils:bash [2024/03/25 16:01] – [Barre de progression] bn8 | informatique:outils:bash [2024/10/30 14:07] (Version actuelle) – [Array] bn8 | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== Bash ====== | ====== Bash ====== | ||
+ | |||
+ | ===== Substitution dans les variables ===== | ||
+ | |||
+ | * Chercher/ | ||
+ | * Pour chercher explicitement **au début** du contenu de la variable : '' | ||
+ | * Pour chercher explicitement **à la fin** du contenu de la variable : '' | ||
+ | * Pour remplacer **toutes les occurences** : '' | ||
+ | * Mise en **majuscule** (upper case) : '' | ||
+ | * Pour mettre **tout en majuscule** : '' | ||
+ | * Mise en **minuscule** (lower case) : '' | ||
+ | * Pour mettre **tout en minuscule** : '' | ||
===== Array ===== | ===== Array ===== | ||
- | * déclaration : '' | + | * déclaration : '' |
+ | * déclaration d'un tableau associatif : '' | ||
+ | * déclaration d'un tableau en lecture seule : '' | ||
* ajouter un élément : '' | * ajouter un élément : '' | ||
- | * lister tous les éléments : '' | + | |
- | * récupérer le nombre d'élement | + | |
+ | * lister toutes les clés d'un tableau associatif : '' | ||
+ | * récupérer le nombre d'élements d'un tableau | ||
+ | * construire un tableau à partir d'une chaîne de caractères : Cela dépend du séparateur utilisé : | ||
+ | * avec un retour à la ligne : '' | ||
+ | * avec un espace (ou autre caractère unique et " | ||
+ | * ajouter des valeurs à un tableau existant : '' | ||
+ | * ajouter depuis un fichier : '' | ||
+ | * ajouter depuis la sortie d'une commande : '' | ||
+ | * Note : voir la fonction '' | ||
==== Fonctions utiles ==== | ==== Fonctions utiles ==== | ||
Ligne 45: | Ligne 67: | ||
is_empty $array && echo empty | is_empty $array && echo empty | ||
! is_empty $array && echo not empty | ! is_empty $array && echo not empty | ||
+ | </ | ||
+ | |||
+ | === array_filter === | ||
+ | |||
+ | <code bash> | ||
+ | function array_filter() { | ||
+ | local values=() x=0 v | ||
+ | for v in " | ||
+ | if [[ " | ||
+ | x=1 | ||
+ | elif [[ $x -eq 0 ]]; then | ||
+ | values+=( " | ||
+ | else | ||
+ | mapfile -t values < <( printf ' | ||
+ | fi | ||
+ | done | ||
+ | printf ' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Utilisation :** | ||
+ | <code bash> | ||
+ | a=(a b c d e) | ||
+ | array_filter ${a[@]} -- c e | ||
+ | # Output: | ||
+ | # a | ||
+ | # b | ||
+ | # d | ||
+ | </ | ||
+ | |||
+ | === array_intersect === | ||
+ | |||
+ | <code bash> | ||
+ | function array_intersect() { | ||
+ | local result_var=$1 | ||
+ | declare -ga " | ||
+ | shift | ||
+ | local array1=() | ||
+ | local array2=() | ||
+ | local switch_to_array2=0 | ||
+ | |||
+ | for v in " | ||
+ | if [ " | ||
+ | switch_to_array2=1 | ||
+ | elif [ $switch_to_array2 -eq 0 ]; then | ||
+ | array2+=( " | ||
+ | else | ||
+ | array1+=( " | ||
+ | fi | ||
+ | done | ||
+ | |||
+ | for i in " | ||
+ | for j in " | ||
+ | if [[ $i == $j ]]; then | ||
+ | declare -ga " | ||
+ | break | ||
+ | fi | ||
+ | done | ||
+ | done | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Utilisation :** | ||
+ | <code bash> | ||
+ | a=(a b c d e) | ||
+ | b=(c d) | ||
+ | array_intersect c " | ||
+ | echo " | ||
+ | # Result: | ||
+ | c d | ||
</ | </ | ||
Ligne 68: | Ligne 160: | ||
# - 2 | # - 2 | ||
# - 3 | # - 3 | ||
+ | </ | ||
+ | |||
+ | === explode === | ||
+ | |||
+ | <code bash> | ||
+ | function explode() { | ||
+ | local output_var=$1 seperator=$2 | ||
+ | declare -ga " | ||
+ | mapfile -t " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Utilisation :** | ||
+ | <code bash> | ||
+ | explode myarray " " "1 2 3" "4 5" | ||
+ | declare -p myarray | ||
+ | # Output: | ||
+ | # declare -a myarray=([0]=" | ||
+ | |||
+ | explode myarray " | ||
+ | 1 | ||
+ | 2 | ||
+ | 3" | ||
+ | declare -p myarray | ||
+ | # Output: | ||
+ | # declare -a myarray=([0]=" | ||
</ | </ | ||
Ligne 74: | Ligne 192: | ||
<code bash> | <code bash> | ||
function format_duration { | function format_duration { | ||
- | | + | |
- | local D=$((T/ | + | local d=$((t/ |
- | local H=$((T/ | + | local h=$((t/ |
- | local M=$((T/60%60)) | + | local m=$((t/60%60)) |
- | local S=$((T%60)) | + | local s=$((t%60)) |
- | | + | |
- | printf ' | + | printf ' |
} | } | ||
</ | </ | ||
Ligne 182: | Ligne 300: | ||
function pbar_create() { | function pbar_create() { | ||
# Define the name of the variable that will store the progress bar ID | # Define the name of the variable that will store the progress bar ID | ||
- | [ -n "$4" ] && | + | |
- | | + | |
# Generate the progress bar ID | # Generate the progress bar ID | ||
- | | + | |
# Initialize progress bar information | # Initialize progress bar information | ||
- | PBARS[" | + | PBARS[" |
- | [ -n " | + | [ -n " |
- | [ -n " | + | [ -n " |
- | [ -n " | + | [ -n " |
- | PBARS[" | + | PBARS[" |
- | PBARS[" | + | PBARS[" |
+ | PBARS[" | ||
# Draw the progress bar for a first time | # Draw the progress bar for a first time | ||
- | pbar_draw $ID | + | pbar_draw |
} | } | ||
Ligne 201: | Ligne 320: | ||
# - the ID of the progress bar (default: $PBID) | # - the ID of the progress bar (default: $PBID) | ||
function pbar_finish() { | function pbar_finish() { | ||
- | [ -n "$1" ] && | + | |
- | unset ' | + | |
- | unset ' | + | # Force a last update of the progess bar |
- | unset ' | + | PBARS[" |
- | unset ' | + | pbar_draw " |
- | unset ' | + | |
+ | # Unset progress bar info | ||
+ | unset ' | ||
+ | unset ' | ||
+ | unset ' | ||
+ | unset ' | ||
+ | unset ' | ||
+ | unset ' | ||
echo | echo | ||
} | } | ||
Ligne 214: | Ligne 340: | ||
# - the ID of the progress bar (default: $PBID) | # - the ID of the progress bar (default: $PBID) | ||
# - extra message to display in the progress bar (before the ETA, optional) | # - extra message to display in the progress bar (before the ETA, optional) | ||
+ | # - all extra arguments will be use to compute the extra message using printf | ||
function pbar_draw() { | function pbar_draw() { | ||
- | [ -n "$1" ] && | + | |
+ | |||
+ | # Compute extra message | ||
+ | local extra=${2:-} | ||
+ | # shellcheck disable=SC2059 | ||
+ | [[ -n " | ||
# Only update progress bar one time by second | # Only update progress bar one time by second | ||
- | | + | |
- | [ $NOW -eq ${PBARS[${ID}_LAST_UPDATE]} ] && return | + | [[ "${PBARS[${id}_END_TIME]}" |
# Compute progress percentage | # Compute progress percentage | ||
- | | + | |
- | + | (( perc=${PBARS[${id}_CURRENT]}*100/ | |
- | # Compute duration, total duration, ETA & speed | + | |
- | let DURATION=NOW-${PBARS[${ID}_START_TIME]} | + | |
- | if [ ${PBARS[${ID}_CURRENT]} -gt 0 ] | + | |
- | then | + | |
- | let TOTAL_DURATION=DURATION*${PBARS[${ID}_TOTAL]}/ | + | |
- | SPEED=$( echo " | + | |
- | else | + | |
- | TOTAL_DURATION=0 | + | |
- | SPEED="?" | + | |
- | fi | + | |
- | let ETA=TOTAL_DURATION-DURATION | + | |
# Compute line without the progress bar | # Compute line without the progress bar | ||
- | | + | |
- | " | + | line_items=( |
+ | " | ||
" | " | ||
- | " | + | " |
- | ) | + | |
- | [ -n " | + | |
- | LINE+=( | + | |
- | "- ETA: $(format_duration $ETA)" | + | |
- | "- $( printf "(%s / %s, %s/s)" " | + | |
) | ) | ||
+ | [ -n " | ||
+ | |||
+ | # Add ETA (or total duration if finish) | ||
+ | if [[ " | ||
+ | # Compute duration, total duration, ETA & speed | ||
+ | local duration total_duration speed eta | ||
+ | (( duration=now-${PBARS[${id}_START_TIME]} )) | ||
+ | if [[ " | ||
+ | (( total_duration=duration*${PBARS[${id}_TOTAL]}/ | ||
+ | speed=$( bc <<< | ||
+ | else | ||
+ | total_duration=0 | ||
+ | speed="?" | ||
+ | fi | ||
+ | (( eta=total_duration-duration )) | ||
+ | |||
+ | line_items+=( | ||
+ | "- ETA: $(format_duration $eta)" | ||
+ | "- $( printf "(%s / %s, %s/s)" " | ||
+ | ) | ||
+ | else | ||
+ | local total_duration | ||
+ | (( total_duration=${PBARS[${id}_END_TIME]}-${PBARS[${id}_START_TIME]} )) | ||
+ | line_items+=( "- Total duration: $(format_duration $total_duration)" | ||
+ | fi | ||
# Compute progress bar length (if not configured) | # Compute progress bar length (if not configured) | ||
- | read -r TERM_HEIGHT TERM_WIDTH | + | |
- | | + | |
- | if [ $SIZE -eq 0 ] | + | |
- | | + | if [[ "$size" |
- | | + | |
- | | + | |
- | [ $SIZE -lt 5 ] && | + | |
fi | fi | ||
# Set progress bar text | # Set progress bar text | ||
- | | + | |
- | | + | |
- | | + | |
# Add line padding (if need) | # Add line padding (if need) | ||
- | | + | |
- | [ $LINE_PAD | + | |
# Compute & display line (strip the terminal width) | # Compute & display line (strip the terminal width) | ||
- | | + | |
- | echo -en "\r${LINE:0:$TERM_WIDTH}" | + | echo -en "\r${line:0:$term_width}" |
# Update last progress bar update time | # Update last progress bar update time | ||
- | PBARS[${ID}_LAST_UPDATE]=$NOW | + | PBARS[${id}_LAST_UPDATE]=$now |
} | } | ||
Ligne 281: | Ligne 423: | ||
# - all extra arguments will be use to compute the extra message using printf | # - all extra arguments will be use to compute the extra message using printf | ||
function pbar_increment() { | function pbar_increment() { | ||
- | | + | |
- | # Compute extra message | + | |
- | if [ -n "$2" ] | + | |
- | then | + | |
- | EXTRA=" | + | |
- | shift 2 | + | |
- | [ $# -gt 0 ] && | + | |
- | else | + | |
- | EXTRA="" | + | |
- | fi | + | |
# Increment the progress bar state | # Increment the progress bar state | ||
- | ((PBARS[${ID}_CURRENT]++)) | + | ((PBARS[${id}_CURRENT]++)) |
# Draw the progress bar | # Draw the progress bar | ||
- | pbar_draw | + | pbar_draw "$@" |
} | } | ||
</ | </ | ||
Ligne 301: | Ligne 434: | ||
<code bash> | <code bash> | ||
pbar_create " | pbar_create " | ||
- | for i in $( seq 1 20 ) | + | for i in $( seq 1 20 ); do |
- | do | + | |
pbar_increment | pbar_increment | ||
sleep 0.1 | sleep 0.1 | ||
done | done | ||
pbar_finish | pbar_finish | ||
+ | </ | ||
+ | |||
+ | **Ajout d'info avant l'ETA :** | ||
+ | <code bash> | ||
+ | pbar_create " | ||
+ | for i in $( seq 1 20 ); do | ||
+ | pbar_increment "" | ||
+ | sleep 0.1 | ||
+ | done | ||
+ | pbar_finish "" | ||
</ | </ | ||
<note warning> | <note warning> |