Recently I was looking for a "countdown alarm clock" in Linux and decided to write a Bash script to do the job. The goal was to allow the user to enter a target date and time, then display a clock with the remaining time. An example of the output is below.
Features:
Implementation:
It might seem complicated to do this, but the Unix 'date' utility handles almost all of the program logic for us. For example, to convert a generic date string into the number of seconds from Jan 1st, 1970:
date -d "DATE STRING" +%s
The date command can then easily do the reverse operation, using the output of the first command, where "SECONDS" is number of seconds from Jan 1st, 1970:
date -d @SECONDS
Some screen drawing tricks were accomplished with special terminal escape commands. These are used to move the cursor out of the way, and clear the line before each update.
Code: countdown.sh #!/bin/bash
BLINK=1
INPUT_DATE=$@
STOP=`date -d "$INPUT_DATE" +%s`
[ $? == 0 ] || exit -1
function print_time
{
echo -ne "\033]0;$@\007" # update window title
echo -ne "$@" # update terminal
}
clear
while [ 1 ]; do
NOW=`date +%s`
TIME=''
if [ $STOP -gt $NOW ]; then
REMAIN=$[ $STOP - $NOW ]
YEAR=`date -u -d @$REMAIN +%Y`
YEAR=$[ $YEAR - 1970 ]
[ $YEAR -gt 0 ] && TIME="${TIME}$YEAR year"
[ $YEAR -gt 1 ] && TIME="${TIME}s"
[ $YEAR -gt 0 ] && TIME="${TIME}, "
MONTH=`date -u -d @$REMAIN +%_m`
MONTH=$[ $MONTH - 1 ]
[ $MONTH -gt 0 ] && TIME="${TIME}$MONTH month"
[ $MONTH -gt 1 ] && TIME="${TIME}s"
[ $MONTH -gt 0 ] && TIME="${TIME}, "
DAY=`date -u -d @$REMAIN +%_d`
DAY=$[ $DAY - 1 ]
[ $DAY -gt 0 ] && TIME="${TIME}$DAY day"
[ $DAY -gt 1 ] && TIME="${TIME}s"
[ $DAY -gt 0 ] && TIME="${TIME}, "
TIME="${TIME}`date -u -d @$REMAIN +%T`"
print_time $TIME
else
[ $BLINK -eq 1 ] && print_time "00:00:00"
[ $BLINK -eq 0 ] && print_time ""
BLINK=$[ $BLINK ^ 1 ]
fi
echo -ne "\e[s" # save cursor position
echo -ne "\e[1000;1000f" # move cursor to bottom right of screen
sleep 0.5
echo -ne "\e[u" # restore cursor position
echo -ne "\r" # move cursor to start of line
echo -ne "\e[0K" # clear line
done
.... Read more!
Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts
Saturday, February 9, 2008
Countdown Alarm
Subscribe to:
Posts (Atom)