was wondering if there was a script that would countdiwn the days until christmas
#!/bin/sh
XMAS=`date -d "Dec 25" +%j`
TODAY=`date +%j`
DAYS=$(($XMAS - $TODAY))
if [[ ! $DAYS =~ ^[0-9+$ ]]; then
echo There are $DAYS days left until Xmas.
else
echo Merry Xmas and Happy New year\!
fi
This kind of cheats. If it's a negative number it assumes that it's after christmas and will just print the second line. you could do some more logic to make it return the actual number until the next christmas, but I didn't want to gum it up too much.
This script uses Julian Days. This is overkill if you have GNU date (Linux)
#!/bin/ksh
jd=0
fraction=0
JD() # $1==day $2=month $3=year
{
day=$1
month=$2
year=$3
jd=0
if [[ $month -lt 3 ]] ; then
year=$(( $year - 1 ))
month=$(( $month + 12 ))
fi
a=$(( $year / 100 ))
b=$(( $a / 4 ))
c=$(( 2 - $a + $b ))
e=$(echo "365.25 * ( 4716 + $year ) " | bc -l)
f=$(echo "30.6001 * ( $month + 1)" | bc -l )
if [[ $year -gt 1581 ]] ; then
jd=$(( $c + $day + $e + $f ))
jd=$( echo "$jd - 1524.5" | bc -l )
fi
export jd
}
hr=$(( $(date "+%H") * 3600 ))
min=$(( $(date "+%M") *60 ))
sec=$(date "+%S")
fraction=$( echo "$hr/86400 + $min/86400 + $sec/86400"| bc -l)
JD $( date "+%d %m %Y" )
today=$( echo "$jd + $fraction" | bc -l)
yr=$( date "+%Y" )
JD 25 12 $yr
xmas=$jd
difference=$(( $xmas - $today ))
if [[ $difference -lt 0 ]] ; then
yr=$(( $yr + 1 ))
JD 25 12 $yr
xmas=$jd
difference=$(( $xmas - $today ))
fi
echo "Days to Christmas: $difference"
With Gnu date...
echo $(($(date -d 25-Dec +%j) - $(date +%j))) days until xmas