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/20 17:27] – [Fonctions utiles] bn8 | informatique:outils:bash [2024/03/25 16:01] (Version actuelle) – [Barre de progression] bn8 | ||
---|---|---|---|
Ligne 70: | Ligne 70: | ||
</ | </ | ||
+ | === format_duration === | ||
+ | <code bash> | ||
+ | function format_duration { | ||
+ | local T=$1 | ||
+ | local D=$((T/ | ||
+ | local H=$((T/ | ||
+ | local M=$((T/ | ||
+ | local S=$((T%60)) | ||
+ | (( $D > 0 )) && printf '%d days and ' $D | ||
+ | printf ' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === dump_ass_array === | ||
+ | |||
+ | <code bash> | ||
+ | function dump_ass_array() { | ||
+ | declare -n aarr=" | ||
+ | echo " | ||
+ | for key in " | ||
+ | printf ' | ||
+ | done | ||
+ | echo " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Utilisation :** | ||
+ | <code bash> | ||
+ | declare -A myarray | ||
+ | myarray[a]=" | ||
+ | myarray[c]=" | ||
+ | dump_ass_array myarray | ||
+ | </ | ||
+ | |||
+ | === var_dump === | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | ===== Gestion des paramètres ===== | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | |||
+ | DEBUG=0 | ||
+ | BIN_PATH="/ | ||
+ | EXTRA_ARGS=() | ||
+ | |||
+ | function usage() { | ||
+ | error=" | ||
+ | [ -n " | ||
+ | cat << EOF | ||
+ | Usage : $(basename $0) [-d] [-b / | ||
+ | -b [path] | ||
+ | -d Debug mode | ||
+ | -X Enable bash tracing (=set -x) | ||
+ | -h Show this message | ||
+ | EOF | ||
+ | [ -n " | ||
+ | exit 0 | ||
+ | } | ||
+ | |||
+ | function debug() { | ||
+ | [ $DEBUG -eq 1 ] && >&2 echo -e "$( date ' | ||
+ | } | ||
+ | |||
+ | idx=1 | ||
+ | while [ $idx -le $# ] | ||
+ | do | ||
+ | OPT=${!idx} | ||
+ | case $OPT in | ||
+ | -d) | ||
+ | DEBUG=1 | ||
+ | ;; | ||
+ | -h) | ||
+ | usage | ||
+ | ;; | ||
+ | -b) | ||
+ | ((idx++)) | ||
+ | BIN_PATH=${!idx} | ||
+ | if [ ! -x " | ||
+ | then | ||
+ | usage " | ||
+ | fi | ||
+ | ;; | ||
+ | -X) | ||
+ | set -x | ||
+ | ;; | ||
+ | *) | ||
+ | EXTRA_ARGS+=( $OPT ) | ||
+ | ;; | ||
+ | esac | ||
+ | ((idx++)) | ||
+ | done | ||
+ | |||
+ | debug "Extra args: ${EXTRA_ARGS[@]}" | ||
+ | </ | ||
+ | |||
+ | ===== Barre de progression ===== | ||
+ | |||
+ | <code bash> | ||
+ | declare -A PBARS | ||
+ | declare PBID | ||
+ | |||
+ | # Create a progress bar | ||
+ | # Arguments: | ||
+ | # - the progress bar title (default: Progress) | ||
+ | # - total count (default: 100) | ||
+ | # - bar size (default: use all the width of the terminal with a minimum of 5 caracters) | ||
+ | # - the name of the variable use to store the progress bar ID (default: PBID) | ||
+ | function pbar_create() { | ||
+ | # Define the name of the variable that will store the progress bar ID | ||
+ | [ -n " | ||
+ | declare -n ID=" | ||
+ | # Generate the progress bar ID | ||
+ | ID=$( tr -dc A-Za-z0-9 </ | ||
+ | # Initialize progress bar information | ||
+ | PBARS[" | ||
+ | [ -n " | ||
+ | [ -n " | ||
+ | [ -n " | ||
+ | PBARS[" | ||
+ | PBARS[" | ||
+ | # Draw the progress bar for a first time | ||
+ | pbar_draw $ID | ||
+ | } | ||
+ | |||
+ | # Finish a progress bar | ||
+ | # Arguments: | ||
+ | # - the ID of the progress bar (default: $PBID) | ||
+ | function pbar_finish() { | ||
+ | [ -n " | ||
+ | unset ' | ||
+ | unset ' | ||
+ | unset ' | ||
+ | unset ' | ||
+ | unset ' | ||
+ | echo | ||
+ | } | ||
+ | |||
+ | # Draw the progress bar | ||
+ | # Arguments: | ||
+ | # - the ID of the progress bar (default: $PBID) | ||
+ | # - extra message to display in the progress bar (before the ETA, optional) | ||
+ | function pbar_draw() { | ||
+ | [ -n " | ||
+ | |||
+ | # Only update progress bar one time by second | ||
+ | NOW=$(date +%s) | ||
+ | [ $NOW -eq ${PBARS[${ID}_LAST_UPDATE]} ] && return | ||
+ | |||
+ | # Compute progress percentage | ||
+ | let 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 | ||
+ | LINE=( | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ) | ||
+ | [ -n " | ||
+ | LINE+=( | ||
+ | "- ETA: $(format_duration $ETA)" | ||
+ | "- $( printf "(%s / %s, %s/s)" " | ||
+ | ) | ||
+ | |||
+ | # Compute progress bar length (if not configured) | ||
+ | read -r TERM_HEIGHT TERM_WIDTH < <(stty size) | ||
+ | SIZE=${PBARS[${ID}_SIZE]} | ||
+ | if [ $SIZE -eq 0 ] | ||
+ | then | ||
+ | LINE_LENGTH=$( echo " | ||
+ | SIZE=$[ $TERM_WIDTH - $LINE_LENGTH ] | ||
+ | [ $SIZE -lt 5 ] && SIZE=5 | ||
+ | fi | ||
+ | |||
+ | # Set progress bar text | ||
+ | let DONE=$PERC*$SIZE/ | ||
+ | let LEFT=$SIZE-$DONE | ||
+ | LINE[1]=" | ||
+ | |||
+ | # Add line padding (if need) | ||
+ | let LINE_PAD=TERM_WIDTH-${# | ||
+ | [ $LINE_PAD -gt 0 ] && LINE+=( " | ||
+ | |||
+ | # Compute & display line (strip the terminal width) | ||
+ | LINE=" | ||
+ | echo -en " | ||
+ | |||
+ | # Update last progress bar update time | ||
+ | PBARS[${ID}_LAST_UPDATE]=$NOW | ||
+ | } | ||
+ | |||
+ | # Increment the progress bar | ||
+ | # Arguments: | ||
+ | # - the ID of the progress bar (default: $PBID) | ||
+ | # - 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_increment() { | ||
+ | [ -n " | ||
+ | # Compute extra message | ||
+ | if [ -n " | ||
+ | then | ||
+ | EXTRA=" | ||
+ | shift 2 | ||
+ | [ $# -gt 0 ] && EXTRA=$( printf " | ||
+ | else | ||
+ | EXTRA="" | ||
+ | fi | ||
+ | # Increment the progress bar state | ||
+ | ((PBARS[${ID}_CURRENT]++)) | ||
+ | # Draw the progress bar | ||
+ | pbar_draw $ID " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Utilisation :** | ||
+ | <code bash> | ||
+ | pbar_create " | ||
+ | for i in $( seq 1 20 ) | ||
+ | do | ||
+ | pbar_increment | ||
+ | sleep 0.1 | ||
+ | done | ||
+ | pbar_finish | ||
+ | </ | ||
+ | |||
+ | <note warning> |