![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| awk calculation | kekanap | Shell Programming and Scripting | 4 | 11-17-2006 04:39 AM |
| Specify a previous date as start date in shell script | ritzwan0 | Shell Programming and Scripting | 2 | 09-25-2006 02:58 PM |
| date calculation program | axes | High Level Programming | 1 | 09-12-2006 07:11 PM |
| script execution time calculation | johnsonbryce | Shell Programming and Scripting | 9 | 02-24-2006 10:33 PM |
| Math calculation help | sickboy | Shell Programming and Scripting | 4 | 06-11-2005 10:22 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Date calculation script
I often need to calculate yesterday's date to parse log files. Thus, i came up with this script. I felt it would be useful for others, so I am pasting it here. Any suggestions or bug fixes are welcome. It does basic error checking for coomand line parameters entered.
Code:
#! /bin/sh
###########################################################################################
## This scripts runs in 2 ways #
## First way: without command line arguments. It calculates yesterday's date #
## Second way: 3 numeric argumnets(dd mm yyyy). Calculates date prior to the input date
###########################################################################################
case $# in
3)
[[ `echo $1 | grep [[:alpha:]]` || `echo $1 | grep [[:punct:]]` ]] && echo -e "$1 is not a number.\nQuitting" && exit 1
[[ `echo $2 | grep [[:alpha:]]` || `echo $2 | grep [[:punct:]]` ]] && echo -e "$2 is not a number.\nQuitting" && exit 1
[[ `echo $3 | grep [[:alpha:]]` || `echo $3 | grep [[:punct:]]` ]] && echo -e "$3 is not a number.\nQuitting" && exit 1
YEAR=$3
NMONTH=$2
DATE=$1
# Add a 0 in front if month or date is single digit; i.e. < 10
[[ ${#NMONTH} -eq 1 ]] && NMONTH="0$NMONTH"
[[ ${#DATE} -eq 1 ]] && DATE="0$DATE"
[[ $DATE > 31 || $DATE < 01 ]] && echo -e "Invalid date.\n Quitting" && exit 1
[[ $NMONTH > 12 || $NMONTH < 01 ]] && echo -e "Invalid month.\n Quitting" && exit 1
[[ ${#YEAR} -ne 4 ]] && echo -e "Invalid Year.\n Quitting" && exit 1
;;
*)
YEAR=`date +"%Y"`
NMONTH=`date +"%m"`
DATE=`date +"%d"`;;
esac
if [[ $DATE == 01 || $DATE == 1 ]]; then # If its the first day of the month, set yesterday's date accordingly.
case $NMONTH in
01|02|04|06|08|09|11)
DATE=31
if [[ $NMONTH == "01" || $NMONTH == "1" ]]; then
YEAR=`expr $YEAR - 1`
NMONTH=12;LMONTH="December"
else
NMONTH=`expr $NMONTH - 1`
fi;;
03) if [[ `expr $YEAR % 4` -eq 0 ]]; then
DATE=29
else
DATE=28
fi
NMONTH=`expr $NMONTH - 1`;;
*)
DATE=30
NMONTH=`expr $NMONTH - 1` ;;
esac
else
DATE=`expr $DATE - 1`
fi
# If numeric month or date is <10, add a 0 in front
if [ ${#NMONTH} -eq 1 ]; then
NMONTH="0$NMONTH"
fi
if [ ${#DATE} -eq 1 ]; then
DATE="0$DATE"
fi
case $NMONTH in # set long month name
01) LMONTH="January";;
02) LMONTH="February";;
03) LMONTH="March";;
04) LMONTH="April";;
05) LMONTH="May";;
06) LMONTH="June";;
07) LMONTH="July";;
08) LMONTH="August";;
09) LMONTH="September";;
10) LMONTH="October";;
11) LMONTH="November";;
12) LMONTH="December";;
esac
echo $DATE-$NMONTH-$YEAR #Output date-month-year
Last edited by vish_indian; 07-31-2006 at 05:25 AM. Reason: Added code tags |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Pls edit this post and put your code in Code tags, it'll be more readable or moderators can do, Thanks for sharing the code here. Regards, Tayyab
|
|
#3
|
||||
|
||||
|
Quote:
Code:
[/tmp]$ ./try.sh 31 04 2006 30-04-2006 [/tmp]$ ./try.sh 31 02 2006 30-02-2006 [/tmp]$ |
|
#4
|
||||
|
||||
|
I have linked this thread into the faq article. It's always good to have another alternative.
|
|
#5
|
|||
|
|||
|
Quote:
One thing that has caused me lot of trouble is being forced to take the values(date,month) as strings. If i treat them as numerics and check them using -eq instead of ==, i get an error for values 08 and 09. Same error comes up while using the syntax (( date-- )). I am still trying to figure out why that happens. |
|
#6
|
||||
|
||||
|
Quote:
|
|
#7
|
|||
|
|||
|
Quote:
Thanks, that did helped. |
|||
| Google The UNIX and Linux Forums |