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/07/15 13:52] – [Array] bn8 | informatique:outils:bash [2024/10/30 14:07] (Version actuelle) – [Array] bn8 | ||
---|---|---|---|
Ligne 7: | Ligne 7: | ||
* Pour chercher explicitement **à la fin** du contenu de la variable : '' | * Pour chercher explicitement **à la fin** du contenu de la variable : '' | ||
* Pour remplacer **toutes les occurences** : '' | * Pour remplacer **toutes les occurences** : '' | ||
- | * Mise en **majuscule** : '' | + | * Mise en **majuscule** |
* Pour mettre **tout en majuscule** : '' | * Pour mettre **tout en majuscule** : '' | ||
- | * Mise en **minuscule** : '' | + | * Mise en **minuscule** |
* Pour mettre **tout en minuscule** : '' | * Pour mettre **tout en minuscule** : '' | ||
Ligne 22: | Ligne 22: | ||
* lister toutes les clés d'un tableau associatif : '' | * lister toutes les clés d'un tableau associatif : '' | ||
* récupérer le nombre d' | * récupérer le nombre d' | ||
+ | * 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 95: | Ligne 102: | ||
function array_intersect() { | function array_intersect() { | ||
local result_var=$1 | local result_var=$1 | ||
- | declare -g " | + | declare -ga " |
shift | shift | ||
local array1=() | local array1=() | ||
Ligne 114: | Ligne 121: | ||
for j in " | for j in " | ||
if [[ $i == $j ]]; then | if [[ $i == $j ]]; then | ||
- | declare -g " | + | declare -ga " |
break | break | ||
fi | fi | ||
Ligne 126: | Ligne 133: | ||
a=(a b c d e) | a=(a b c d e) | ||
b=(c d) | b=(c d) | ||
- | array_filter | + | array_intersect |
- | echo ${c[@]} | + | echo "${c[@]}" |
# Result: | # Result: | ||
c d | c d | ||
Ligne 153: | 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 267: | 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 286: | 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 299: | 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 366: | 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 386: | 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> |