View: Barre de progression à largeur automatique

  1. 1 month ago by benoitbalon
    1. #!/bin/sh
    2.  
    3. # ex :
    4. # ~$ test.sh 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    5. # [||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||] 100.00%
    6.  
    7. barre_progression ()
    8. {
    9.         nb_iterations=$1
    10.         liste_length=$2
    11.         pourcent=`echo "scale=2;($nb_iterations * 100) / $liste_length" | bc`
    12.         pourcent=`printf "% 3.2f" "$pourcent"`
    13.        
    14.         chaine="$pourcent"
    15.         nb_etoiles=`echo "scale=0;$chaine / $diviseur_barre" | bc`
    16.         etoiles=""
    17.         while test $nb_etoiles -gt 0
    18.         do
    19.                 etoiles="${caractere_barre}${etoiles}"
    20.                 nb_etoiles=`echo "$nb_etoiles - 1" | bc`
    21.         done
    22.         etoiles=`printf "[%-${largeur_barre}s]" "$etoiles"`
    23.         pourcentage=`printf "%7s" "$chaine"`
    24.         #echo "\r${etoiles}${pourcentage}%\r\c"
    25.         printf "%b%b%b" "\r${etoiles}" "${pourcentage}" "%\r\c"
    26. }
    27.  
    28. # INITIALISATION DE LA BARRE DE PROGRESSION
    29. init_progress_bar ()
    30. {
    31.         # caractere qui sera affiche dans la barre de progression
    32.         caractere_barre="|"
    33.        
    34.         # detection de l'OS
    35.         os=`uname -s | tr [a-z] [A-Z]`
    36.        
    37.         # recuperation de la largeur en caracteres du terminal
    38.         if test "$os" = "LINUX"
    39.         then
    40.                 largeur_ecran=`stty -a | grep columns | cut -d ' ' -f7 | tr -d ';'`
    41.         elif test "$os" = "SUNOS"
    42.         then
    43.                 largeur_ecran=`stty | grep columns | cut -d ' ' -f6 | tr -d ';'`
    44.         else
    45.                 echo "Systeme d'exploitation incompatible, programme stoppe"
    46.                 exit 1
    47.         fi
    48.        
    49.         # calcul de la largeur optimale de la barre de progression
    50.         largeur_barre=`echo "$largeur_ecran - 7 - 2 - 1" | bc`
    51.         diviseur_barre=`echo "scale=2;100 / $largeur_barre" | bc`
    52.        
    53.         # protection des variables
    54.         readonly largeur_ecran largeur_barre diviseur_barre caractere_barre
    55. }
    56.  
    57. # gestion de la langue de l'environnement
    58. LC_NUMERIC=C
    59.  
    60. init_progress_bar
    61.  
    62. liste_length=$#
    63. nb_iterations=0
    64. if test $liste_length -gt 0
    65. then
    66.         for i in $@
    67.         do
    68.                 nb_iterations=`echo "$nb_iterations + 1" | bc`
    69.                 barre_progression "$nb_iterations" "$liste_length"
    70.                 # sleep 1
    71.         done
    72. fi
    73.  
    74. echo " "

2 comments about "Barre de progression à largeur automatique"

  1. Lorsque l'environnement est en français cela crée des erreurs du type : ./test.sh: line 14: printf: 33.33: invalid number (standard_in) 1: parse error ./test.sh: line 19: test: -gt: unary operator expected En ajoutant LC_NUMERIC=C au début du script cela résoud le problème.
    un_nain_connu on June 02, 2008
  2. Merci pour l'info, un_nain_connu ! J'ai ajouté LC_NUMERIC=C sur ton bon conseil.
    benoitbalon on June 03, 2008