Shell Script for Leap Year

Hello every body,
i have below a nice script to tell year the year is Leap or NOT.just copy the following line into a new file name it leap.ksh

# Shell program to read any year and find whether leap year or not
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project 
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# store year
yy=0
isleap="false"
#echo -n "Enter year (yyyy) : "
#read yy
yy=$1
# find out if it is a leap year or not
if [ $((yy % 4)) -ne 0 ] ; then
   : #  not a leap year : means do nothing and use old value of isleap
elif [ $((yy % 400)) -eq 0 ] ; then
   # yes, it's a leap year
   isleap="true"
elif [ $((yy % 100)) -eq 0 ] ; then
   : # not a leap year do nothing and use old value of isleap
else
   # it is a leap year
   isleap="true"
fi
if [ "$isleap" == "true" ];
then
   echo "YES"
else
   echo "NOT"
fi

Script Using:

#./leap.ksh 2013

 

HINT : you can change 2013 with any year you want, or you can send it using variables. also you can edit script to make it take the year from user (interactive script)

This entry was posted in Shell Script and tagged , , . Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.