Adsense Leaderboard Ad

1.31.2015

How To Make a Countdown Timer in Bash

Question sent in by Stephen from Texas

Q: I am writing a bash script that opens a port for two hours and then closes it.  I use sleep 7200 to make it wait two hours before closing the port.  This works fine, but when I walk away it is hard for me to know when the port will be closed.  I wanted to make a little countdown to show how much longer the script will take to run.  I found some code on the net but couldn't get any of them to work for me.

A: I am sure there are plenty of ways to make a countdown, but here is what I came up with. In the second section I commented the code to help you understand what each line does.

#!/bin/bash
hour=2
min=0
sec=0
       while [ $hour -ge 0 ]; do
                while [ $min -ge 0 ]; do
                        while [ $sec -ge 0 ]; do
                                echo -ne "$hour:$min:$sec\033[0K\r"
                                let "sec=sec-1"
                                sleep 1
                        done
                        sec=59
                        let "min=min-1"
                done
                min=59
                let "hour=hour-1"
        done

Here is the code with quotes above each line to explain how it works:
#!/bin/bash
# Set variables to your desired time.
hour=2
min=0
sec=0
       # begin hour while loop - while hour
       #variable is greater than or equal to 0 do minute loop
while [ $hour -ge 0 ]; do
                # begin minute loop - while min variable
                #is greater than or equal to 0 do second loop
                while [ $min -ge 0 ]; do
                        # begin second loop - while sec variable is greater
                        # than or equal to 0 print time left
                        while [ $sec -ge 0 ]; do
                                # echo time on same line so it overwrites last                                             # line, makes it look like countdown
                                echo -ne "$hour:$min:$sec\033[0K\r"
                                # Decrease the sec variable by 1
                                #each iteration of loop to countdown
                                let "sec=sec-1"
                                # wait a second before removing a second
                                # from the countdown clock
                                sleep 1
                        # End second loop
                        done
                        # Set second timer back to 59 to start new minute
                        sec=59
                        # Decrease min variable by 1 to remove a
                        # minute off the countdown
                        let "min=min-1"
                # end minute loop
                done
                # Set minute timer back to 59 to start new hour
                min=59
                # decrease the hour by 1 to remove hour off the countdown
                let "hour=hour-1"
        # end hour loop
        done 


Works well, I may even use this in a couple scripts that I have.

1 comment:

  1. excellent script. i changed the echo a bit to add a leading zero, when appropriate:

    echo -ne "$(printf "%02d" $hour):$(printf "%02d" $min):$(printf "%02d" $sec)\033[0K\r"

    ReplyDelete