## KSH Leap Year Calculation

## KSH Leap Year Calculation

(OP)

OK, OK, I know it's been beaten to death, but I came up with a cool way to test for leap year in Korn Shell.

Basically it uses '

#### CODE

#!/bin/ksh typeset -i IS_LEAP=$( cal 2 $(date '+%Y') | grep 29 | wc -l ) (( IS_LEAP )) && print "This is a leap year!" if (( ! IS_LEAP )) then print "NOT a leap year!" fi

Basically it uses '

**cal**' to get a calendar of February of this year (simple mod to get other years),**grep**s that one month calendar for a '29', then counts how many rows the grep output. If it's a leap year, IS_LEAP will equal 1. If it's not a leap year, IS_LEAP will be a 0. So IS_LEAP can be used as a boolean as in the example code.
## RE: KSH Leap Year Calculation

there's not much of "kshisms".

this almost could be (arithmetic evaluation excepted, and variable's typing that is not essential) a POSIX script as well.

the script is fine, not the title of the subject.

## RE: KSH Leap Year Calculation

## RE: KSH Leap Year Calculation

daPeach: Yup, you're correct.stefanhei: Good point. Didn't think it through I guess.## CODE