Last active
February 27, 2016 18:04
-
-
Save acetwenty8/b192bd322114bb844e16 to your computer and use it in GitHub Desktop.
leapyear.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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