informatique:outils:bash

Différences

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

Lien vers cette vue comparative

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:37] – [Gestion des paramètres] bn8informatique:outils:bash [2024/03/25 16:01] (Version actuelle) – [Barre de progression] bn8
Ligne 69: Ligne 69:
 # - 3 # - 3
 </code> </code>
 +
 +=== format_duration ===
 +
 +<code bash>
 +function format_duration {
 +  local T=$1
 +  local D=$((T/60/60/24))
 +  local H=$((T/60/60%24))
 +  local M=$((T/60%60))
 +  local S=$((T%60))
 +  (( $D > 0 )) && printf '%d days and ' $D
 +  printf '%02d:%02d:%02d' $H $M $S
 +}
 +</code>
 +
 +=== dump_ass_array ===
 +
 +<code bash>
 +function dump_ass_array() {
 +        declare -n aarr="$1"
 +        echo "\"$1\" = {"
 +        for key in "${!aarr[@]}"; do
 +                printf '  "%s" => "%s"\n' "$key" "${aarr[$key]}"
 +        done
 +        echo "}"
 +}
 +</code>
 +
 +**Utilisation :**
 +<code bash>
 +declare -A myarray
 +myarray[a]="b"
 +myarray[c]="d"
 +dump_ass_array myarray
 +</code>
 +
 +=== var_dump ===
 +
 +<code bash>declare -p variableName</code>
  
 ===== Gestion des paramètres ===== ===== Gestion des paramètres =====
Ligne 128: Ligne 167:
 debug "Extra args: ${EXTRA_ARGS[@]}" debug "Extra args: ${EXTRA_ARGS[@]}"
 </code> </code>
 +
 +===== 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 "$4" ] && PBAR_ID_VAR="$4" || PBAR_ID_VAR=PBID
 +    declare -n ID="$PBAR_ID_VAR"
 +    # Generate the progress bar ID
 +    ID=$( tr -dc A-Za-z0-9 </dev/urandom | head -c 3 )
 +    # Initialize progress bar information
 +    PBARS["${ID}_START_TIME"]="$( date +%s )"
 +    [ -n "$1" ] && PBARS["${ID}_TITLE"]="$1" || PBARS["${ID}_TITLE"]="Progress"
 +    [ -n "$2" ] && PBARS["${ID}_TOTAL"]="$2" || PBARS["${ID}_TOTAL"]=100
 +    [ -n "$3" ] && PBARS["${ID}_SIZE"]="$3" || PBARS["${ID}_SIZE"]=0
 +    PBARS["${ID}_CURRENT"]=0
 +    PBARS["${ID}_LAST_UPDATE"]=0
 +    # 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 "$1" ] && ID=$1 || ID=$PBID
 +    unset 'PBARS[${ID}_START_TIME]'
 +    unset 'PBARS[${ID}_TITLE]'
 +    unset 'PBARS[${ID}_TOTAL]'
 +    unset 'PBARS[${ID}_CURRENT]'
 +    unset 'PBARS[${ID}_LAST_UPDATE]'
 +    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 "$1" ] && ID=$1 || ID=$PBID
 +
 +    # 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/${PBARS[${ID}_TOTAL]}
 +
 +    # 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]}/${PBARS[${ID}_CURRENT]}
 +        SPEED=$( echo "scale=1; ${PBARS[${ID}_CURRENT]}/$DURATION"|bc )
 +    else
 +        TOTAL_DURATION=0
 +        SPEED="?"
 +    fi
 +    let ETA=TOTAL_DURATION-DURATION
 +
 +    # Compute line without the progress bar
 +    LINE=(
 +        "${PBARS[${ID}_TITLE]}"
 +        "[]"
 +        "${PBARS[${ID}_CURRENT]}/${PBARS[${ID}_TOTAL]} (${PERC}%)"
 +    )
 +    [ -n "$2" ] && LINE+=( "- $2" )
 +    LINE+=(
 +        "- ETA: $(format_duration $ETA)"
 +        "- $( printf "(%s / %s, %s/s)" "$(format_duration $DURATION)" "$(format_duration $TOTAL_DURATION)" $SPEED )"
 +    )
 +
 +    # 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 "${LINE[*]}"|wc -c )
 +        SIZE=$[ $TERM_WIDTH - $LINE_LENGTH ]
 +        [ $SIZE -lt 5 ] && SIZE=5
 +    fi
 +
 +    # Set progress bar text
 +    let DONE=$PERC*$SIZE/100
 +    let LEFT=$SIZE-$DONE
 +    LINE[1]="[$(printf "%${DONE}s"|tr ' ' '#')$(printf "%${LEFT}s"|tr ' ' '-')]"
 +
 +    # Add line padding (if need)
 +    let LINE_PAD=TERM_WIDTH-${#LINE}
 +    [ $LINE_PAD -gt 0 ] && LINE+=( "$(printf "%${LINE_PAD}s")" )
 +
 +    # Compute & display line (strip the terminal width)
 +    LINE="${LINE[*]}"
 +    echo -en "\r${LINE:0:$TERM_WIDTH}"
 +
 +    # 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 "$1" ] && ID=$1 || ID=$PBID
 +    # Compute extra message
 +    if [ -n "$2" ]
 +    then
 +        EXTRA="$2"
 +        shift 2
 +        [ $# -gt 0 ] && EXTRA=$( printf "$EXTRA" "$@" )
 +    else
 +        EXTRA=""
 +    fi
 +    # Increment the progress bar state
 +    ((PBARS[${ID}_CURRENT]++))
 +    # Draw the progress bar
 +    pbar_draw $ID "$EXTRA"
 +}
 +</code>
 +
 +**Utilisation :**
 +<code bash>
 +pbar_create "Test" 20
 +for i in $( seq 1 20 )
 +do
 +    pbar_increment
 +    sleep 0.1
 +done
 +pbar_finish
 +</code>
 +
 +<note warning>La fonction [[#format_duration]] est nécessaire.</note>
  • informatique/outils/bash.1710956235.txt.gz
  • Dernière modification : 2024/03/20 17:37
  • de bn8