Skip to content

Instantly share code, notes, and snippets.

@tetafro
Created August 21, 2016 14:09
Show Gist options
  • Save tetafro/b4b2eb8611f1abb5f9647a22caaa3553 to your computer and use it in GitHub Desktop.
Save tetafro/b4b2eb8611f1abb5f9647a22caaa3553 to your computer and use it in GitHub Desktop.
Bash script template with progressbar
#!/bin/bash
# Show progress bar
function show_progress {
end=$1
msg=$2
# Spaces to hide previous message
spaces=' '
for i in `seq 1 4 $end`; do
echo -n █
done
if [ $end -lt 100 ]; then
for i in `seq $(($end+1)) 4 100`; do
echo -n ░
done
echo -n -e " $end% ($msg)$spaces\r"
else
echo -e " $end% ($msg)$spaces"
fi
}
# Start timer
time_start=$((`date +%s`*1000+`date +%-N`/1000000))
show_progress 0 'Do first thing'
do_first_thing
show_progress 40 'Do second thind'
do_second_thing
show_progress 80 'Do third thing'
do_third_thing
show_progress 100 'Complete'
# Calculate time
time_end=$((`date +%s`*1000+`date +%-N`/1000000))
time_progress_ms=$(($time_end - $time_start)) # miliseconds
time_progress=$(echo "scale=3; $time_progress_ms/1000" | bc | sed 's/^\./0./') # seconds
echo "Time in progress: $time_progress sec"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment