A Christmas pattern background with white ASCII text that says Christmas Countdown

A Bash Countdown To Christmas

  • Adam Douglas

Well trying to sleep last night I came up with the festive idea to post about how to create a countdown Bash script for Christmas day. This feels random to me as I really have no reason to do this other than to have fun I suppose. I’m sure many have done this, but I have never created one before so why not. I may learn something new, and hopefully, you may too.

Countdown Script

The countdown script displays the total weeks, days, hours, minutes and seconds until Christmas day. Optionally one can provide a parameter “figlet” to change the display from text to ASCII artwork using the Figlet application. The script will run until the event date is reached, December 25 of the current year. Feel free to press CTRL-C to quit the script earlier. I realize this script could easily be modified to accept parameters for the event date, title, font, etc. For now though, the focus is just one simple date, December 25.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env bash
eventdate="$(date -d 'Dec 25' +%s)"
declare -a labels=("weeks" "days" "hours" "mins" "secs")
title="Christmas Countdown"

setLabels() {
    if [ "$diffweeks" -le 1 ]; then
        labels[0]="week"
    fi
    if [ "$diffdays" -le 1 ]; then
        labels[1]="day"
    fi
    if [ "$diffhours" -le 1 ]; then
        labels[2]="hour"
    fi
    if [ "$diffmins" -le 1 ]; then
        labels[3]="min"
    fi
    if [ "$diffsecs" -le 1 ]; then
        labels[4]="sec"
    fi
}
checkCmdExists() {
    if ! [ -x "$(command -v "$1")" ]
    then
        cmdNotFound="$cmdNotFound $1,"
    fi

    if [[ $cmdNotFound != '' ]]
    then
        quit "Error: The following command(s) where not found:${cmdNotFound::-1}."
    fi
}
displayCountdown() {
    while [ "$((eventdate-$(date +%s)-1))" -gt 0 ]; do
        clear
        secsleft="$((eventdate-$(date +%s)))"
        diffweeks="$((secsleft / 604800))"
        diffdays="$((secsleft / 86400))"
        diffhours="$((secsleft % 86400 / 3600))"
        diffmins="$((secsleft % 3600 / 60))"
        diffsecs="$((secsleft % 60))"
        setLabels

        if [ "$1" = "figlet" ]
        then
            checkCmdExists "figlet"
            figlet -ct "$title"
            figlet -ct "$diffweeks ${labels[0]}"
            figlet -ct "$diffdays ${labels[1]}"
            figlet -ct "$diffhours ${labels[2]}"
            figlet -ct "$diffmins ${labels[3]}"
            figlet -ct "$diffsecs ${labels[4]}"
        else
            printf "%s\n" "$title"
            printf "%s ${labels[0]} %s ${labels[1]} %s ${labels[2]} %s ${labels[3]} %s ${labels[4]}\n" $diffweeks $diffdays $diffhours $diffmins $diffsecs
        fi

        printf "\n%s\n" "Press CTRL-C to quit"
        sleep 1
    done
}
quit() {
    if [[ "$1" = 0 ]]
    then
            exit 0
    else
            printf "%b\n" "$1"
            exit 1
    fi
}

displayCountdown "$1"
quit 0

Install Script

  1. Copy and paste the script above into a desired text editor.
  2. Save the script with the name “countdown” to a desired location.
  3. Set the script execute permission.

     $ chmod +x countdown
    
  4. Verify script permission.

     $ ls -al countdown
    
     -rwxr-xr-x 1 adam adam 2593 Dec  8 17:37 countdown
    

Run Script

In the terminal or console run the script as shown below in default mode or Figlet mode.

$ ./countdown
Christmas Countdown
2 weeks 16 days 6 hours 9 mins 50 secs

Press CTRL-C to quit
$ ./countdown figlet

Countdown to Christmas script displayed in ASCII artwork using Figlet

This is post 47 of 100, and is round 2 of the 100 Days To Offload challenge.

    • fix spelling and grammar
    • change topic
    • change 100DaysToOffload message