countdown to christmas


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting countdown to christmas
# 1  
Old 04-22-2009
countdown to christmas

was wondering if there was a script that would countdiwn the days until christmas
# 2  
Old 04-23-2009
Code:
#!/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.
# 3  
Old 04-23-2009
This script uses Julian Days. This is overkill if you have GNU date (Linux)
Code:
#!/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"

# 4  
Old 04-23-2009
With Gnu date...
Code:
echo $(($(date -d 25-Dec +%j) - $(date +%j))) days until xmas

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Banner Countdown Timer

Hello. I am pretty new to unix and shell scripting and I was wondering if there might be a way to banner a countdown timer inside a script. We currently have an existing script that does a 2 minute sleep but thought it might be fun to actually make it banner a countdown timer until it is finished.... (3 Replies)
Discussion started by: thumbelina
3 Replies

2. Ubuntu

Countdown timer with seconds

I would like this to work with seconds as well. #!/bin/bash # if ; then echo "Incorrect usage ! Example:" echo './CountDown.sh -d "Jun 10 2011 16:06"' echo 'or' echo './CountDown.sh -m 90' exit 1 fi now=`date +%s` if ; then until=`date -d... (7 Replies)
Discussion started by: drew77
7 Replies

3. Shell Programming and Scripting

Perl script countdown

In the below bash when the perl is it possible to hide the commands from running on screen and display a process countdown? For example, on the cygwin screen now the user sees each process in the command running as running protocol refGene, running protocol popfreq_all, etc... Could a... (0 Replies)
Discussion started by: cmccabe
0 Replies

4. Shell Programming and Scripting

Bash - countdown timer

Hello, I have another problem with my script - I would like to have a countdown timer visible on the screen, and at the same time, I want te be able to do something else. And when the time runs out, I need to know about that inside the script somehow and do some action. I guess that would require 2... (3 Replies)
Discussion started by: xqwzts
3 Replies

5. Shell Programming and Scripting

Anyone know of any FUN countdown script

Hi all, Does anyone know of any FUN countdown script that I can use for my script? At the moment, am just using sleep 10 or more and then print stuff into the screen to allow more time for the user to decide whether they want to continue running the script or abort? Just thought of wanting... (3 Replies)
Discussion started by: newbie_01
3 Replies

6. Shell Programming and Scripting

Stop! (the countdown!) :-) shell script help

Hi guys, I've found two nifty little scripts on these forums one which detects if the F5 key has been pressed: #/bin/sh _key() { local kp ESC=$'\e' _KEY= read -d '' -sn1 _KEY case $_KEY in "$ESC") while read -d '' -sn1 -t1 kp do _KEY=$_KEY$kp ... (0 Replies)
Discussion started by: rich@ardz
0 Replies

7. UNIX for Dummies Questions & Answers

countdown in unix?

is there a script to preform a countdown from 10 seconds to 0? (5 Replies)
Discussion started by: JamieMurry
5 Replies

8. Shell Programming and Scripting

Display runnning countdown in a bash script?

I am looking for a way to display on a single line, a running countdown for a given amount of time in a terminal using a bash script. I am looking for this to use as part of a larger bash script that captures Video. The script sets up a bunch of parameters for DVgrab, and one of the parameters... (11 Replies)
Discussion started by: Starcast
11 Replies

9. UNIX for Dummies Questions & Answers

shell script - loop to countdown

I am taking a class in UNIX and have written a script that needs to countdown from a number that is read in from the keyboard to zero. If no number is given the start of the countdown should default to 10. I can't get this to do the default #! /bin/sh echo Enter a number here to countdown... (2 Replies)
Discussion started by: froggwife
2 Replies
Login or Register to Ask a Question