Hi,

I have the below script which pulls information a system.

timeval=5
timer=0
sec=5

while [ $timer -lt $timeval ]
    do
        clear

        now=$(date +'%d %b %Y | %H:%M:%S')
        while [ $sec -ge 0 ]; do
           echo -ne "Updating in $sec\033[0K\r" <-------
           let "sec=sec-1"
           sleep 1
        done

        podname="4g-du"
        kubectl get pods -A | grep $podname | awk 'NR==1 {print "NAMESPACE","NAME","READY","STATUS","RESTARTS"} {print $1" "$2" "$3" "$4" "$5}' | column -t
        echo "********************************************"
        timer=$(( $timer + 1 ))
        sleep 10
done

This runs in a while loop and the Sleep 10 command at the bottom causes the script to wait 10 seconds before the loop is run again.

I inserted a countdown timer in the script where the arrow is pointing <------- so a timer starts when 5 seconds remain.

How can I run the countdown timer because what happens is that the script does not continue until the countdown timer has ended.

Is there a better way of doing this ?

Thank You

5 Spice ups

You messed up the order on where you put the while loop, i think the problem you’re facing is that its waiting 15 seconds instead of 10 right?

timeval=5
timer=0
sec=5

while [ $timer -lt $timeval ]
    do
        clear

        now=$(date +'%d %b %Y | %H:%M:%S')
        

        podname="4g-du"
        kubectl get pods -A | grep $podname | awk 'NR==1 {print "NAMESPACE","NAME","READY","STATUS","RESTARTS"} {print $1" "$2" "$3" "$4" "$5}' | column -t
        echo "********************************************"
        timer=$(( $timer + 1 ))

        sleep 5 # Since the total time you want to wait is 10s, you wait 5 seconds here, then you start the countdown that takes 5 seconds to run
        while [ $sec -ge 0 ]; do
           echo -ne "Updating in $sec\033[0K\r" # <------- (Commented out the arrow)
           let "sec=sec-1"
           sleep 1
        done
done
1 Spice up

Just to add - representing date by pretty much anything other than largest unit to smallest is confusing and bad practice.
If you are logging data and use grep (or anything like sed, awk, perl, python…), the output is then in non-time order. All the data from the firsts of the month, by month Apr, Aug, Dec, … Oct, Sep. then by year.
If you use

TZ=America/New_York date -Iseconds
2024-09-12T13:43:30-04:00

You get something that can be more easily used in scripts;

# Everything from May of this year;
grep -E '^2024-05" SomeLogFile

If you need to compare that to sites around the globe, use;

TZ=UTC date -Iseconds
2024-09-12T17:33:44+00:00

If timezone info isn’t needed, then the lovely;

date +"%F %T"
2024-09-12 13:34:02

Is perfect.
Apologies, this is one of my soap boxes, I’ll see myself out…

4 Spice ups

Hi,

Not exactly, what the script is suppose to do is the below part is suppose to run the outer loop 5 times with a 10 second wait between every run (I’m using Sleep 10 for this)…

podname="4g-du"
        kubectl get pods -A | grep $podname | awk 'NR==1 {print "NAMESPACE","NAME","READY","STATUS","RESTARTS"} {print $1" "$2" "$3" "$4" "$5}' | column -t
        echo "********************************************"
        timer=$(( $timer + 1 ))

Soon as the Sleep reaches 5 seconds of wait, the countdown timer should start counting down Updating in in the same line…

An output from your script…

Your script is fine except that it only shows Updating in once, then the outer loop continues without the Updating in

Found the issue…

The fact the inner while loop runs once, and the condition is met, and the loop never exits, it never check again if sec=5.

I revised the code as below, and now it works as expected…

timeval=5
timer=0

while [ $timer -lt $timeval ]
    do
        clear

        now=$(date +'%d %b %Y | %H:%M:%S')

        podname="4g-du"
        kubectl get pods -A | grep $podname | awk 'NR==1 {print "NAMESPACE","NAME","READY","STATUS","RESTARTS"} {print $1" "$2" "$3" "$4" "$5}' | column -t
        echo "********************************************"
        timer=$(( $timer + 1 ))

        sleep 5
        sec=5 # <----- added the variable inside so it can be checked again, and again
        while [ $sec -ge 0 ]; do
           echo -ne "Updating in $sec\033[0K\r" # <------- (Commented out the arrow)
           let "sec=sec-1"
           sleep 1
        done
done

Thanks @plary