Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Bash Progress Wheel

DZone's Guide to

Bash Progress Wheel

·
Free Resource
// print a progress wheel


#!/bin/bash
#
# anim.sh
# print a progress wheel while test_pad returns 0
#
# $ANIM  : wheel
# $PAD   : on what $ANIM moves
# $SLEEP : timer
#
# Author : Nicolas Marciniak 
  
   



function test_pad()
{
        return 0
}

function reverse()
{
        string=$1
        len=$(echo -n $string | wc -c)
        while test $len -gt 0
        do
                rev=$rev$(echo $string | cut -c $len)
                len=$(( len - 1 ))
        done
        echo $rev
}

ANIM="//--\\||"
PAD="                                                                  "
SLEEP="0.1"

ANIM_L="$(reverse $ANIM)"
PAD_LN=${#PAD}
ANIM_LN=${#ANIM}
u=0; re=0; l=0

while test_pad; do
        if [ $re -eq 1 ]; then L=$ANIM_L; else L=$ANIM; fi
        if [ $re -eq 1 ]; then p=$(( $PAD_LN - $u )); else p=$u; fi
        echo -ne "${PAD:0:$p} ${L:$l:1} ${PAD:$p:$PAD_LN} \r"
        if [ $u -eq $PAD_LN ]; then u=0; if [ $re -eq 1 ]; then re=0; else re=1; fi; fi
        u=$(( u + 1 )); l=$(( l + 1 ))
        if [ $l -eq $ANIM_LN ]; then l=0; fi
        sleep $SLEEP
done
exit 0



  
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}