Progress Indicator-Falling deck of cards(Bash)

bashshell-script

I coded a progress indicator in the form of a falling deck of cards (my wife's idea).

It's not in its final form yet. Here is the code:

vdeck="| | | | | "
hdeck[1]="__ | | | |"
hdeck[2]="__ __ | | |"
hdeck[3]="__ __ __ | |"
hdeck[4]="__ __ __ __ |"
hdeck[5]="__ __ __ __ __"

while true
do
    clear
    echo -n -e "\r$vdeck"
    sleep 0.8

    for i in 1 2 3 4 5
    do
        echo -n -e "\r${hdeck[$i]}"
        sleep 0.2
    done
    clear
done

Is it possible to optimize this code without having a hdeck array and getting the same effect? Increasing '|' means increasing the 'hdeck' array combinations, which isn't good.

Best Answer

  • Something like this?

    #!/bin/bash
    LEN=5
    echo 'start';
    for (( i=0; i<((${LEN}+1)); i++ )); do
    echo -ne '\r' 
            for (( j=0; j<${i}; j++ )); do
                     echo -n '_'
            done
            for (( j=${i}; j<${LEN}; j++ )); do
                     echo -n '|'
            done
    sleep 0.5
    done
    echo -e '\ndone'
    
  • Related Question