Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
informatique:outils:bash [2024/10/30 14:07] – [Array] bn8 | informatique:outils:bash [2025/08/21 12:47] (Version actuelle) – [Barre de progression] bn8 | ||
---|---|---|---|
Ligne 31: | Ligne 31: | ||
==== Fonctions utiles ==== | ==== Fonctions utiles ==== | ||
- | |||
=== in_array === | === in_array === | ||
Ligne 186: | Ligne 185: | ||
# Output: | # Output: | ||
# declare -a myarray=([0]=" | # declare -a myarray=([0]=" | ||
+ | </ | ||
+ | |||
+ | === check_regex === | ||
+ | |||
+ | <code bash> | ||
+ | function check_regex() { | ||
+ | [[ $(grep -Ec " | ||
+ | return 1 | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === check_int === | ||
+ | |||
+ | **Pré-requis :** [[# | ||
+ | |||
+ | <code bash> | ||
+ | function check_int() { | ||
+ | check_regex " | ||
+ | [[ -n " | ||
+ | [[ -n " | ||
+ | return 0 | ||
+ | } | ||
</ | </ | ||
Ligne 199: | Ligne 220: | ||
[[ $d -gt 0 ]] && printf '%d days and ' $d | [[ $d -gt 0 ]] && printf '%d days and ' $d | ||
printf ' | printf ' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === format_size === | ||
+ | |||
+ | **Pré-requis :** [[# | ||
+ | |||
+ | <code bash> | ||
+ | declare -ra _FORMAT_SIZE_UNITS=( tb gb mb kb b ) | ||
+ | declare -rA _FORMAT_SIZE_UNITS_FACTOR=( | ||
+ | [" | ||
+ | function format_size() { | ||
+ | local size="" | ||
+ | for opt in " | ||
+ | opt=" | ||
+ | if [[ ${#opt} -gt 2 ]] && \ | ||
+ | [[ " | ||
+ | unit=${opt: | ||
+ | elif [[ ${#opt} -gt 1 ]] && \ | ||
+ | [[ " | ||
+ | unit=${opt: | ||
+ | elif [[ " | ||
+ | allow_zero=1 | ||
+ | elif [[ -z " | ||
+ | size=$opt | ||
+ | [[ " | ||
+ | check_int " | ||
+ | else | ||
+ | echo -n " | ||
+ | return 1 | ||
+ | fi | ||
+ | done | ||
+ | if [[ $size -eq 0 ]]; then | ||
+ | [[ $allow_zero -eq 0 ]] && return | ||
+ | echo -n " | ||
+ | elif [[ $size -lt 0 ]]; then | ||
+ | (( size=size*-1 )) | ||
+ | negative=1 | ||
+ | fi | ||
+ | |||
+ | (( size=size*${_FORMAT_SIZE_UNITS_FACTOR[$unit]} )) | ||
+ | for unit in " | ||
+ | [[ $size -lt ${_FORMAT_SIZE_UNITS_FACTOR[$unit]} ]] && continue | ||
+ | if [[ $size -eq ${_FORMAT_SIZE_UNITS_FACTOR[$unit]} ]]; then | ||
+ | size=1 | ||
+ | else | ||
+ | size=$( echo " | ||
+ | fi | ||
+ | [[ $negative -eq 1 ]] && size=$( echo " | ||
+ | echo -n " | ||
+ | return | ||
+ | done | ||
} | } | ||
</ | </ | ||
Ligne 227: | Ligne 300: | ||
<code bash> | <code bash> | ||
+ | === sprint === | ||
+ | |||
+ | **Pré-requis :** [[# | ||
+ | |||
+ | <code bash> | ||
+ | # | ||
+ | # Styled text printing helper | ||
+ | # | ||
+ | declare -rA COLORS=( | ||
+ | [black]=30 [red]=31 [green]=32 [brown]=33 [blue]=34 [purple]=35 [cyan]=36 [light_grey]=37 | ||
+ | [default]=39 [dark_grey]=90 [light_red]=91 [light_green]=92 [yellow]=93 [light_blue]=94 | ||
+ | [light_purple]=95 [light_cyan]=96 [white]=97 | ||
+ | ) | ||
+ | declare -rA BACKGROUND_COLORS=( | ||
+ | [black]=40 [red]=41 [green]=42 [brown]=43 [blue]=44 [purple]=45 [cyan]=46 [ligth_grey]=47 | ||
+ | [default]=49 [dark_grey]=100 [light_red]=101 [light_green]=102 [yellow]=103 [light_blue]=104 | ||
+ | [light_purple]=105 [light_cyan]=106 [white]=107 | ||
+ | ) | ||
+ | declare -rA TEXT_STYLES=( | ||
+ | [normal]=0 [bold]=1 [dim]=2 [italic]=3 [underline]=4 [blink]=5 [inverted_colors]=7 [hidden]=8 | ||
+ | [strikethrough]=9 | ||
+ | ) | ||
+ | declare -rA RESET_STYLES=( | ||
+ | [all]=0 [bold]=21 [dim]=22 [underline]=24 [blink]=25 [inverted_colors]=27 [hidden]=28 | ||
+ | ) | ||
+ | |||
+ | function sprint() { | ||
+ | local idx=1 opt value no_newline=0 output="" | ||
+ | local -a styles=() text=() | ||
+ | |||
+ | __sprint() { | ||
+ | [[ -n " | ||
+ | [[ " | ||
+ | output+=" | ||
+ | styles=() | ||
+ | output+=" | ||
+ | text=() | ||
+ | } | ||
+ | while [[ $idx -le $# ]]; do | ||
+ | opt=${!idx} | ||
+ | case $opt in | ||
+ | -c|--color) | ||
+ | [[ " | ||
+ | ((idx++)) | ||
+ | value=" | ||
+ | if [[ " | ||
+ | echo -n " | ||
+ | return 1 | ||
+ | fi | ||
+ | styles+=( " | ||
+ | ;; | ||
+ | -b|--bg) | ||
+ | [[ " | ||
+ | ((idx++)) | ||
+ | value=" | ||
+ | if [[ " | ||
+ | echo -n " | ||
+ | return 1 | ||
+ | fi | ||
+ | styles+=( " | ||
+ | ;; | ||
+ | -s|--style) | ||
+ | [[ " | ||
+ | ((idx++)) | ||
+ | value=" | ||
+ | if [[ " | ||
+ | echo -n " | ||
+ | return 1 | ||
+ | fi | ||
+ | styles+=( " | ||
+ | ;; | ||
+ | -r|--reset) | ||
+ | [[ " | ||
+ | ((idx++)) | ||
+ | value=" | ||
+ | if [[ " | ||
+ | echo -n " | ||
+ | return 1 | ||
+ | fi | ||
+ | output+=" | ||
+ | ;; | ||
+ | -n) | ||
+ | no_newline=1 | ||
+ | ;; | ||
+ | -h|--help) | ||
+ | echo " | ||
+ | echo " | ||
+ | implode ", " " | ||
+ | fold -w 53 -s | sed " | ||
+ | echo | ||
+ | echo " | ||
+ | implode ", " " | ||
+ | fold -w 53 -s | sed " | ||
+ | echo | ||
+ | echo " | ||
+ | implode ", " " | ||
+ | fold -w 53 -s | sed " | ||
+ | echo | ||
+ | echo " | ||
+ | implode ", " " | ||
+ | fold -w 53 -s | sed " | ||
+ | echo | ||
+ | echo " | ||
+ | echo " | ||
+ | ;; | ||
+ | *) | ||
+ | text+=( " | ||
+ | esac | ||
+ | ((idx++)) | ||
+ | done | ||
+ | __sprint | ||
+ | output+=" | ||
+ | if [[ " | ||
+ | echo -en " | ||
+ | else | ||
+ | echo -e " | ||
+ | fi | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Exemple :** | ||
+ | <code bash> | ||
+ | sprint -c red ' | ||
+ | </ | ||
+ | |||
+ | Résultat : | ||
+ | |||
+ | {{: | ||
===== Gestion des paramètres ===== | ===== Gestion des paramètres ===== | ||
Ligne 290: | Ligne 491: | ||
<code bash> | <code bash> | ||
declare -A PBARS | declare -A PBARS | ||
- | declare PBID | ||
# Create a progress bar | # Create a progress bar | ||
Ligne 297: | Ligne 497: | ||
# - total count (default: 100) | # - total count (default: 100) | ||
# - bar size (default: use all the width of the terminal with a minimum of 5 caracters) | # - 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: | + | # - the name of the variable use to store the progress bar ID (default: |
function pbar_create() { | function pbar_create() { | ||
- | | + | local id=${4:-PBAR} |
- | | + | |
- | local -n id=" | + | |
- | # Generate the progress bar ID | + | |
- | id="$( tr -dc A-Za-z0-9 </ | + | |
# Initialize progress bar information | # Initialize progress bar information | ||
PBARS[" | PBARS[" | ||
Ligne 318: | Ligne 514: | ||
# Finish a progress bar | # Finish a progress bar | ||
# Arguments: | # Arguments: | ||
- | # - the ID of the progress bar (default: | + | # - the ID of the progress bar (default: |
function pbar_finish() { | function pbar_finish() { | ||
- | local id=${1:-}; [[ -z " | + | local id=${1:-PBAR} |
# Force a last update of the progess bar | # Force a last update of the progess bar | ||
Ligne 338: | Ligne 534: | ||
# Draw the progress bar | # Draw the progress bar | ||
# Arguments: | # Arguments: | ||
- | # - the ID of the progress bar (default: | + | # - the ID of the progress bar (default: |
# - 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 | # - all extra arguments will be use to compute the extra message using printf | ||
function pbar_draw() { | function pbar_draw() { | ||
- | local id=${1:-}; [[ -z " | + | local id=${1:-PBAR} |
# Compute extra message | # Compute extra message | ||
Ligne 419: | Ligne 615: | ||
# Increment the progress bar | # Increment the progress bar | ||
# Arguments: | # Arguments: | ||
- | # - the ID of the progress bar (default: | + | # - the step (default: 1) |
+ | # - the ID of the progress bar (default: | ||
# - 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 | # - all extra arguments will be use to compute the extra message using printf | ||
function pbar_increment() { | function pbar_increment() { | ||
- | local id=${1:-}; [[ -z " | + | local step=${1:-1} id=${2:-PBAR} |
# Increment the progress bar state | # Increment the progress bar state | ||
- | ((PBARS[${id}_CURRENT]++)) | + | ((PBARS[${id}_CURRENT]+=step)) |
# Draw the progress bar | # Draw the progress bar | ||
- | pbar_draw " | + | pbar_draw "${@:2}" |
} | } | ||
</ | </ | ||
Ligne 445: | Ligne 642: | ||
pbar_create " | pbar_create " | ||
for i in $( seq 1 20 ); do | for i in $( seq 1 20 ); do | ||
- | pbar_increment "" | + | pbar_increment |
sleep 0.1 | sleep 0.1 | ||
done | done | ||
- | pbar_finish "" | + | pbar_finish |
</ | </ | ||
<note warning> | <note warning> |