Skip to content

Instantly share code, notes, and snippets.

@ldante86
Last active November 17, 2016 23:10
Show Gist options
  • Save ldante86/c2a174952341fca02abc664997c75589 to your computer and use it in GitHub Desktop.
Save ldante86/c2a174952341fca02abc664997c75589 to your computer and use it in GitHub Desktop.
A simple binary clock
#!/bin/bash -
SCRIPT: binary-clock
AUTHOR: Luciano D. Cecere
YEAR: 2016
PROGRAM="${0##*/}"
format="\n\t%s\t%s\n"
_dec_to_bin()
{
local decimal=$1
local bit binary
while [ $decimal -ne 0 ]
do
((bit=decimal % 2))
binary=$bit$binary
((decimal/=2))
done
while ((${#binary} < 9))
do
binary="0$binary"
done
echo $binary | sed "s/0/ /g" | sed "s/1/ 0 /g"
}
_get_hour() { date +%_$x; }
_get_minute() { date +%_M; }
_get_second() { date +%_S; }
_clock()
{
while clear
do
local h=$(_get_hour)
local m=$(_get_minute)
local s=$(_get_second)
printf "\n\n"
printf "$format" "$(_dec_to_bin $h)" "$h"
printf "$format" "$(_dec_to_bin $m)" "$m"
printf "$format" "$(_dec_to_bin $s)" "$s"
printf "\n\t\t%s\n" " 32 16 8 4 2 1"
sleep 1
done
}
case $1 in
-h)
echo "Usage: $PROGRAM [12 24 -h]"
exit
;;
24)
x=H
;;
12|*)
x=I
;;
esac
trap 'printf "\e[?12l\e[?25h"' EXIT
printf "\e[?25l"
_clock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment