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
informatique:outils:bash [2024/10/30 14:07] – [Array] bn8informatique:outils:bash [2025/04/24 08:31] (Version actuelle) – [Fonctions utiles] bn8
Ligne 186: Ligne 186:
 # Output: # Output:
 # declare -a myarray=([0]="1" [1]="2" [2]="3") # declare -a myarray=([0]="1" [1]="2" [2]="3")
 +</code>
 +
 +=== check_regex ===
 +
 +<code bash>
 +function check_regex() {
 +  [[ $(grep -Ec "$2" <<< "$1") -eq 1 ]] && return 0
 +  return 1
 +
 +</code>
 +
 +=== check_int ===
 +
 +**Pré-requis :** [[#check_regex]]
 +
 +<code bash>
 +function check_int() {
 +    check_regex "$1" '^-?[0-9]+$' || return 1
 +    [[ -n "$2" ]] && [[ $1 -lt $2 ]] && return 1
 +    [[ -n "$3" ]] && [[ $1 -gt $3 ]] && return 1
 +    return 0
 +}
 </code> </code>
  
Ligne 199: Ligne 221:
     [[ $d -gt 0 ]] && printf '%d days and ' $d     [[ $d -gt 0 ]] && printf '%d days and ' $d
     printf '%02d:%02d:%02d' $h $m $s     printf '%02d:%02d:%02d' $h $m $s
 +}
 +</code>
 +
 +=== format_size ===
 +
 +**Pré-requis :** [[#check_int]]
 +
 +<code bash>
 +declare -ra _FORMAT_SIZE_UNITS=( tb gb mb kb b )
 +declare -rA _FORMAT_SIZE_UNITS_FACTOR=(
 +    ["tb"]=1099511627776 ["gb"]=1073741824 ["mb"]=1048576 ["kb"]=1024 ["b"]=1 )
 +function format_size() {
 +    local size="" unit=kb allow_zero=0 negative=0 opt
 +    for opt in "$@"; do
 +        opt="${opt,,}"
 +        if [[ ${#opt} -gt 2 ]] && \
 +            [[ "${_FORMAT_SIZE_UNITS_FACTOR[${opt:2}]:-null}" != "null" ]]; then
 +            unit=${opt:2}
 +        elif [[ ${#opt} -gt 1 ]] && \
 +            [[ "${_FORMAT_SIZE_UNITS_FACTOR[${opt:1}]:-null}" != "null" ]]; then
 +            unit=${opt:1}
 +        elif [[ "$opt" == "--allow-zero" ]] || [[ "$opt" == "-z" ]]; then
 +            allow_zero=1
 +        elif [[ -z "$size" ]]; then
 +            size=$opt
 +            [[ "$size" == "null" ]] && echo -n null && return
 +            check_int "$size" || { echo -n "format_size: invalid value '$size'"; return 1; }
 +        else
 +            echo -n "format_size: invalid parameter '$opt'"
 +            return 1
 +        fi
 +    done
 +    if [[ $size -eq 0 ]]; then
 +        [[ $allow_zero -eq 0 ]] && return
 +        echo -n "0${_FORMAT_SIZE_UNITS[${#_FORMAT_SIZE_UNITS[@]} - 1]}"
 +    elif [[ $size -lt 0 ]]; then
 +        (( size=size*-1 ))
 +        negative=1
 +    fi
 +
 +    (( size=size*${_FORMAT_SIZE_UNITS_FACTOR[$unit]} ))
 +    for unit in "${_FORMAT_SIZE_UNITS[@]}"; do
 +        [[ $size -lt ${_FORMAT_SIZE_UNITS_FACTOR[$unit]} ]] && continue
 +        if [[ $size -eq ${_FORMAT_SIZE_UNITS_FACTOR[$unit]} ]]; then
 +            size=1
 +        else
 +            size=$( echo "scale=1; $size/${_FORMAT_SIZE_UNITS_FACTOR[$unit]}"|bc|sed 's/\.0$//' )
 +        fi
 +        [[ $negative -eq 1 ]] && size=$( echo "$size*-1"|bc )
 +        echo -n "${size}${unit^^}"
 +        return
 +    done
 } }
 </code> </code>
  • informatique/outils/bash.txt
  • Dernière modification : 2025/04/24 08:31
  • de bn8