Skip to content

Instantly share code, notes, and snippets.

@acetwenty8
Last active February 27, 2016 18:04
Show Gist options
  • Save acetwenty8/b192bd322114bb844e16 to your computer and use it in GitHub Desktop.
Save acetwenty8/b192bd322114bb844e16 to your computer and use it in GitHub Desktop.
leapyear.sh
#!/bin/bash
function_testleap () {
local WORK_YEAR=$1
local IS_LEAP="false"
local IS_100="false"
local IS_400="false"
if [[ $((WORK_YEAR % 4)) -ne 0 ]]; then
: #not a yeap lear
elif [[ $((WORK_YEAR % 400)) -eq 0 ]]; then
local IS_400="true"
elif [[ $((WORK_YEAR % 100)) -eq 0 ]]; then
local IS_100="true"
else
local IS_LEAP="true"
fi
if [[ $IS_LEAP == "true" ]]; then
echo -n 10
elif [[ $IS_400 == "true" ]]; then
echo -n 11
elif [[ $IS_100 == "true" ]]; then
echo -n 1
else
echo -n 0
fi
}
showhelp() {
echo "leapyear.sh v1.0 02/27/2016"
echo "written by @acetwenty8"
echo " -h print this help screen"
echo " -y [start_year] year to start with (default 1752)"
echo " -c [count] number of years to count forward (default 0)"
echo " -100 print non-leap years due to being divisable by 100"
echo " -400 print leap years due to being divisible by 400"
echo ""
exit
}
COUNT=1
START_YEAR=1752
ARGS=$@
while [[ $# > 0 ]]
do
key="$1"
case $key in
-y|--year)
START_YEAR="$2"
shift
;;
-c|--count)
COUNT="$2"
shift
;;
-100)
PRINT_100="1"
;;
-400)
PRINT_400="1"
;;
-h|--help)
SHOW_HELP="1"
;;
*)
;;
esac
shift
done
if [[ $SHOW_HELP -eq 1 ]]; then
showhelp
exit
fi
YEAR=$START_YEAR
if [[ $PRINT_100 -eq 1 ]]; then
RUN=1
VAR=1
elif [[ $PRINT_400 -eq 1 ]]; then
RUN=1
VAR=11
fi
i=0
while [[ $i -le $COUNT ]]; do
result=$(function_testleap $YEAR)
if [[ $RUN -eq 1 ]]; then
if [[ $result -eq $VAR ]]; then
echo $YEAR
fi
else
if [[ $result -ge 10 ]]; then
echo $YEAR
fi
fi
let "i++"
let "YEAR++"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment