Countdown time bash script without using transparent while loop

With a google link for the countdown, I'm trying to make a different conclusion. that is, showing the remaining seconds to continue: 20. Each time the countdown reaches 10 .., it counts down as 90, 80, 70, 60, 50, 40, 30, 20, 10. There are many examples of data using clear in while loop, but I don’t want to clear my scripts of other outputs when displaying the countdown time.

I would like to know where I am making a mistake.

function countdown () 
    { 
        if (($# != 1)) || [[ $1 = *[![:digit:]]* ]]; then
            echo "Usage: countdown seconds";
            return;
        fi;
        local t=$1 remaining=$1;
        SECONDS=0;
        while sleep .2; do
            printf '\rseconds remaining to proceed: '"$remaining";
            if (( (remaining=t-SECONDS) <=0 )); then
                printf '\rseconds remaining to proceed' 0;
                break;
            fi;
        done
    }
+3
source share
1 answer

The problem is that it actually counts 9,8,7,6, etc., but you never wiped the final 0 out of 10, so it stays on the screen. Simple fix:

printf '\rseconds remaining to proceed: '"$remaining"' ';

Add a space to hit 0 from the screen, and all is well.

+7
source

All Articles