Monday, April 19, 2010

Korn (ksh) and Bash shell script for determining leap year

Based on the algorithm in Wikipedia, this script takes a four-digit year as an argument, and determines whether or not it is a leap year. Obviously not super-useful in itself, the "if" line could be incorporated into a larger script. Appears to work in ksh and bash, but not sh.



year=$1

if [ $((year%400)) -eq 0 ] || \
( [ $((year%4)) -eq 0 ] && [ $((year%100)) -ne 0 ] )
then
echo "leap"
else
echo "not leap"
fi

No comments: