[CS-FSLUG] Shell Scripting - one liners
Frank Bax
fbax at sympatico.ca
Sat Jul 31 20:19:47 CDT 2004
At 08:11 PM 7/31/04, Brian Derr wrote:
>On Fri, Jul 30, 2004 at 01:11:10PM -0400, Frank Bax wrote:
> > I remember there were contests at university to solve coding problems with
> > one-liners - some of solutions 'wrapped' to a dozen lines. I remember one
> > question was a one-line formula for some date-related question that had to
> > work for leap years (year divisible by 4 and 400, but not 100).
>
>export year=""; read -p "Year: " year; if [[ `expr $year % 400` -eq 0 ]]
>|| [[ `expr $year % 4` -eq 0 ]] && [[ `expr $year % 100` -ne 0 ]]; then
>echo "leap year..."; else echo "not leap year..."; fi
>
>How about that? :-)
Your solution thinks that years divisible by 400 (like 1600 and 2000) are
not leap years.
For others that might want to see how this works, create a text file (call
it leap). containing the above solution - make sure there's no newlines -
it must be all on one line! Then open a shell and type:
sh leap
Frank
More information about the Christiansource
mailing list