<?xml version="1.0" encoding="UTF-8" ?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
    <title>Posteet: benoitbalon: bc</title> 
    <link>http://www.posteet.com/</link> 
    <description>Recent posteets posted to Posteet</description>
    <ttl>60</ttl>

    
    <item>
        <title>Barre de progression à largeur automatique</title>
        <link>http://www.posteet.com/view/938</link>
        <description>
        <![CDATA[<pre>#!/bin/sh

# ex :
# ~$ test.sh 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# [||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||] 100.00%

barre_progression ()
{
	nb_iterations=$1
	liste_length=$2
	pourcent=`echo &quot;scale=2;($nb_iterations * 100) / $liste_length&quot; | bc`
	pourcent=`printf &quot;% 3.2f&quot; &quot;$pourcent&quot;`
	
	chaine=&quot;$pourcent&quot;
	nb_etoiles=`echo &quot;scale=0;$chaine / $diviseur_barre&quot; | bc`
	etoiles=&quot;&quot;
	while test $nb_etoiles -gt 0
	do
		etoiles=&quot;${caractere_barre}${etoiles}&quot;
		nb_etoiles=`echo &quot;$nb_etoiles - 1&quot; | bc`
	done
	etoiles=`printf &quot;[%-${largeur_barre}s]&quot; &quot;$etoiles&quot;`
	pourcentage=`printf &quot;%7s&quot; &quot;$chaine&quot;`
	#echo &quot;\r${etoiles}${pourcentage}%\r\c&quot;
	printf &quot;%b%b%b&quot; &quot;\r${etoiles}&quot; &quot;${pourcentage}&quot; &quot;%\r\c&quot;
}

# INITIALISATION DE LA BARRE DE PROGRESSION
init_progress_bar ()
{
	# caractere qui sera affiche dans la barre de progression
	caractere_barre=&quot;|&quot;
	
	# detection de l'OS
	os=`uname -s | tr [a-z] [A-Z]`
	
	# recuperation de la largeur en caracteres du terminal
	if test &quot;$os&quot; = &quot;LINUX&quot;
	then
		largeur_ecran=`stty -a | grep columns | cut -d ' ' -f7 | tr -d ';'`
	elif test &quot;$os&quot; = &quot;SUNOS&quot;
	then
		largeur_ecran=`stty | grep columns | cut -d ' ' -f6 | tr -d ';'`
	else
		echo &quot;Systeme d'exploitation incompatible, programme stoppe&quot;
		exit 1
	fi
	
	# calcul de la largeur optimale de la barre de progression 
	largeur_barre=`echo &quot;$largeur_ecran - 7 - 2 - 1&quot; | bc`
	diviseur_barre=`echo &quot;scale=2;100 / $largeur_barre&quot; | bc`
	
	# protection des variables
	readonly largeur_ecran largeur_barre diviseur_barre caractere_barre
}

# gestion de la langue de l'environnement
LC_NUMERIC=C

init_progress_bar

liste_length=$#
nb_iterations=0
if test $liste_length -gt 0
then
	for i in $@ 
	do
		nb_iterations=`echo &quot;$nb_iterations + 1&quot; | bc`
		barre_progression &quot;$nb_iterations&quot; &quot;$liste_length&quot;
		# sleep 1
	done
fi

echo &quot; &quot;</pre> <a href="http://www.posteet.com/tags/bash">[bash]</a>  <a href="http://www.posteet.com/tags/bc">[bc]</a>  <a href="http://www.posteet.com/tags/printf">[printf]</a>  <a href="http://www.posteet.com/tags/shell">[shell]</a>  <a href="http://www.posteet.com/tags/stty">[stty]</a> ]]>        </description>
        <dc:creator>benoitbalon</dc:creator>
        <pubDate>Wed, 07 May 2008 12:44:58 +0000</pubDate>

            <category>bash</category>
            <category>bc</category>
            <category>printf</category>
            <category>shell</category>
            <category>stty</category>
    
    </item>


</channel>
</rss>
