Simple countdown on terminal
In these days I needed a really simple countdown application and, instead of doing a graphical program, I decided to use Bash and some ASCII-art!
The script
First of all we need a Bash script simulating the countdown. We can do it in multiple ways and below is a possible solution:
$ cat clock.sh #!/bin/bash NAME=$(basename $0) if [ $# -lt 2 ] ; then echo "usage: $NAME <min> <sec>" >&2 exit 1 fi min=$1 sec=$2 while sleep 1; do printf "%02d : %02dn" $min $sec if [ $sec -eq 0 -a $min -le 0 ] ; then break; fi sec=$(($sec - 1)) if [ $sec = "-1" ] ; then sec=59 min=$(($min - 1)) fi done echo 'End!!!' exit 0
The script’s structure is simple and we can see how it works by simply executing it as below:
$ ./clock.sh 1 1 01 : 01 01 : 00 00 : 59 00 : 58 00 : 57 00 : 56 ... 00 : 03 00 : 02 00 : 01 00 : 00 End!!!
Now we can enlarge time’s characters by using figlet utility. This tool reads from stdin (or on the command line) a string and reproduces it on stdout using ASCII-art as below:
$ figlet -f big 10 : 30 __ ___ ____ ___ /_ |/ _ \ _ |___ \/ _ \ | | | | | (_) __) | | | | | | | | | |__ <| | | | | | |_| | _ ___) | |_| | |_|\___/ (_) |____/ \___/
The option argument -f big it’s used to select larger fonts than the default.
For our purposes we can send the printf ‘s output to figlet and the trick is done!
The result
Below is a screenshot of my terminal where I enlarged my terminal’s font is such a way the time is displayed as a real digital clock.
Below is the complete code of clock.sh script:
#!/bin/bash NAME=$(basename $0) if [ $# -lt 2 ] ; then echo "usage: $NAME <min> <sec>" >&2 exit 1 fi min=$1 sec=$2 while sleep 1; do printf "%02d : %02dn" $min $sec | figlet -f big if [ $sec -eq 0 -a $min -le 0 ] ; then break; fi sec=$(($sec - 1)) if [ $sec = "-1" ] ; then sec=59 min=$(($min - 1)) fi done echo 'End!!!' | figlet -f big exit 0