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 | |||
informatique:outils:bash [2024/10/30 14:07] – [Array] bn8 | informatique:outils:bash [2025/04/24 08:31] (Version actuelle) – [Fonctions utiles] bn8 | ||
---|---|---|---|
Ligne 186: | Ligne 186: | ||
# 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 221: | ||
[[ $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 | ||
} | } | ||
</ | </ |