I've been playing around implementing some colorful progress bars using shell scripting. I thought it would nice to share my work
If you like em, feel free to use em
Cheers
Code:
#!/bin/sh
# progbar3.sh
# Simulate a progress bar with colors
# Author: Sofiane Cherchalli
# Date: 24 Oct 2010
dot="."
a=( '/' '-' '\' '|' )
per=0
do_sleep() {
local t="$1"
which perl &>/dev/null && sleep_cmd="perl -e 'select(undef,undef,undef,$t)'" || sleep_cmd="sleep $t"
eval "$sleep_cmd"
}
wait_4_esc() {
local msg="Please, press two times ESC key to start demo: "
while read -s -n2 -p "$msg" key; do
printf "\n"
[ "$key" = "`echo $'\x1b\x1b'`" ] && break || msg="What??? I said Press TWO TIMES ESC key on your keyboard to start demo!. Is my english that bad? :( . Try again: "
done
}
# main
wait_4_esc
printf "\nThanks! :) . Starting demo right now...\n"
while [ "$per" -le "100" ]; do
showper=`expr $per % 10`
if [ "$showper" = "0" ]; then
if [ "$per" -le "30" ]; then
printf "\e[31m%s" "${per}%"
elif [ "$per" -gt "30" -a "$per" -le "70" ]; then
printf "\e[33m%s" "${per}%"
else
printf "\e[32m%s" "${per}%"
fi
fi
for ((i=0; i<${#a[@]}; i++)); do
printf "\e[0m%s" "${a[${i}]}"
do_sleep .1
printf "\e[1D\e[0K"
done
[ "$per" -lt "100" ] && printf "\e[0m%s" "$dot"
per=`expr $per + 1`
done
printf "\e[0m\nHYLI!\n\n"
Code:
#!/bin/sh
# progbar6.sh
# Simulate a progress bar with colors
# Author: Sofiane Cherchalli
# Date: 24 Oct 2010
do_sleep() {
local t="$1"
which perl &>/dev/null && sleep_cmd="perl -e 'select(undef,undef,undef,$t)'" || sleep_cmd="sleep $t"
eval "$sleep_cmd"
}
show_prog_bar() {
local c="$1" # Character to use to draw progress bar
local v=$2 # Percentage 0<= value <=100
local pbl=50 # Progress bar length in characters
local r=`expr 100 / $pbl` # Ratio between percentage value and progress bar length
local p="$v%" # Percentage value to display in the middle of progress bar
local l=${#p} # Length of string to display
local pbs="" # Progress bar string
local k=`expr \( $pbl - $l \) / 2` # Position where to substitute in progress bar string
local n1=`expr $v / $r`
local n2=`expr $pbl - $n1`
for (( i=0; i<$pbl; i++ )); do
pbs="${pbs}${c}"
done
pbs=`echo "$pbs" | sed 's/^\(.\{'"${k}"'\}\)\(.\{'"${l}"'\}\)\(.*$\)/\1'"${p}"'\3/'`
printf "\r\e[44m%s\e[41m%s" "${pbs:0:$n1}" "${pbs:$n1:$n2}"
}
# Simulate a child process doing some work. Progress percentage is wtitten to a temp file
some_work() {
local a=(0 3 5 20 37 51 67 80 98 100) # Simulate some progress values done by worker
local sl=(1 .3 .5 3 .5 .9 1 .8 1 3) # Simulate some work times
for ((i=0; i<${#a[@]}; i++)); do
echo "${a[${i}]}" >> /tmp/progbar
do_sleep ${sl[${i}]}
done
}
wait_4_esc() {
local msg="Please, press two times ESC key to start demo: "
while read -s -n2 -p "$msg" key; do
printf "\n"
[ "$key" = "`echo $'\x1b\x1b'`" ] && break || msg="What??? I said Press TWO TIMES ESC key on your keyboard to start demo!. Is my english that bad? :( . Try again: "
done
}
# main
wait_4_esc
> /tmp/progbar
some_work &
pid=$!
per=0
printf "\nThanks! :) . Starting demo right now...\n"
while read line || [ "$per" -lt "100" ]; do
if [ -n "$line" ]; then
per=$line
show_prog_bar " " $per
fi
do_sleep .1
done < /tmp/progbar
printf "\e[0m\nHYLI!\n\n"
Last edited by sofianito; 10-24-2010 at 09:47 PM..
Is there any way to print the colorful message in the terminal (which is the output of a simulation) to the log file with the same color.
The colorful message at the terminal is obtained by the following codes:
/////////////codes start here///////////////////////////////////////
... (5 Replies)
Hi,
I want to send color text as body of mail with multiple attachments.How to do this?I used sendmailcommand but this command fails for multiple attachments.Give me some solution on this.
:wall: (0 Replies)
When I use gnuplot to plot a set of data I get extra y-axis error bars in some random points. Please try the code below, and note the y error bars in the top left corner that don't have any data corresponding to it. Any idea what might be causing this?
gnuplot >
set yrange
set xrange
... (1 Reply)
I know how to create a menu using select and case. What i want to know is,how can i create a menu using select and case where the menu options are in colored text?
---------- Post updated at 02:50 AM ---------- Previous update was at 02:42 AM ----------
if the menu options are like:
1)... (5 Replies)
Maybe someone know how to create something like "*".
If someone uses Gentoo he must see this while emerge utility preparing an update.
I mean the symbol "/" spins. (2 Replies)
I am using this as my command line in Hummingbird Exceed XSession:
xterm -ls -geometry 150x60 -display @D -bg white -fg black
The problem is that I need scroll bars. Any ideas on what to add? (7 Replies)