|
|
|
|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
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 |
| Sponsored Links |
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Date Difference in PERL | usshell | Shell Programming and Scripting | 1 | 05-02-2008 11:57 AM |
| date difference | muay_tb | Linux | 14 | 03-18-2008 10:08 AM |
| Weird date difference problem | meeraKh | Shell Programming and Scripting | 22 | 03-12-2008 11:50 PM |
| date difference | piyush_movadiya | Shell Programming and Scripting | 2 | 08-11-2005 05:10 AM |
| date difference | rajuMBT | UNIX for Dummies Questions & Answers | 1 | 08-10-2005 06:11 AM |