Hi...I need some help with a date script. I need to allow the user to enter the month (alpha) day (int) and year (YYYY) and count the difference in number of days since Jan 1, 1952 to the users date. I've been messing with this for about 10 hours and I think I'm just making the script worse =(
Please help
#!/bin/bash
# grpdif: Number of days between two dates.
# Usage: ./grpdif 01/01/2001
# [M]M/[D]D/YYYY
ARGS=1 # Two command line parameters expected.
E_PARAM_ERR=65 # Param error.
REFYR=1940 # Reference year.
REFMO=1 # Reference month.
REFDY=1 # Reference day.
CENTURY=100
DIY=365
ADJ_DIY=367 # Adjusted for leap year + fraction.
MIY=12
DIM=31
LEAPCYCLE=4
diff= # Declare global variable for date difference.
value= # Declare global variable for absolute value.
day= # Declare globals for day, month, year.
month=
year=
MAXRETVAL=255 # Largest permissable
#+ positive return value from a function.
Param_Error () # Command line parameters wrong.
{
echo "Usage: `basename $0` [M]M/[D]D/YYYY"
exit $E_PARAM_ERR
}
Parse_Date () # Parse date from command line params.
{
month=${1%%/**}
dm=${1%/**} # Day and month.
day=${dm#*/}
let "year = `basename $1`"
}
check_date () # Checks for invalid date(s) passed.
{
[ "$day" -gt "$DIM" ] || [ "$month" -gt "$MIY" ] || [ "$year" -lt "$REFYR" ] && Param_Error
# Exit script on bad value(s).
# Uses "or-list / and-list".
#
let "year=$REFYR"
let “month=$REFMO”
let “day=$REFDY”
let "indexyr = $year / $CENTURY"
let "Days = $DIY*$year + $year/$LEAPCYCLE - $indexyr + $indexyr/ $LEAPCYCLE + $ADJ_DIY*$month/$MIY + $day - $DIM"
echo $Days
}
day_index ()
{ # Days from Jan. 1, 1952 to date passed as param.
day=$1
month=$2
year=$3
let "month = $month - 2"
if [ "$month" -le 0 ]
then
let "month += 12"
let "year -= 1"
fi
let "year -= $REFYR"
let "month -=$REFMO"
let "day -=$REFDY"
let "indexyr = $year / $CENTURY"
let "Days = $DIY*$year + $year/$LEAPCYCLE - $indexyr + $indexyr/$LEAPCYCLE + $ADJ_DIY*$month/$MIY + $day - $DIM"
echo $Days
}
calculate_difference () # Difference between to day indices.
{
let "diff = $1 - $2" # Global variable.
}
if [ $# -ne "$ARGS" ] # Require two command line params.
then
Param_Error
fi
Parse_Date $1
check_date $day $month $year # See if valid date.
day=$? # on day and/or month.
month=$?
let "date1 = `day_index $day $month $year`"
Parse_Date $2
check_date $day $month $year
day=$?
month=$?
let "date2 = $(day_index $day $month $year)"
calculate_difference $date1 $date2
diff=$value
echo $diff
exit 0