The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-31-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 88
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
Reply With Quote
Forum Sponsor
  #2  
Old 07-31-2006
tayyabq8's Avatar
Moderator
 

Join Date: Nov 2004
Location: Bahrain
Posts: 555
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
Reply With Quote
  #3  
Old 07-31-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
Quote:
Originally Posted by vish_indian
Any suggestions or bug fixes are welcome. It does basic error checking for coomand line parameters entered.
Check for boundary conditions for months having 28/29/30/31 days.
Code:
[/tmp]$ ./try.sh 31 04 2006
30-04-2006
[/tmp]$ ./try.sh 31 02 2006
30-02-2006
[/tmp]$
If you haven't see Perderabo's datecalc, then you might want to take a look at that. It does the same thing plus so many other date calculations.
Reply With Quote
  #4  
Old 07-31-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,656
I have linked this thread into the faq article. It's always good to have another alternative.
Reply With Quote
  #5  
Old 07-31-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 88
Quote:
Check for boundary conditions for months having 28/29/30/31 days.
I had ignored that part intentionally for now. My main usage is regarding the system date. I added the command line usage option as an after thought. I added only the basic check that entered values are nos. and are between 1-31 for days and 1-12 for months. Will add month wise day check as time permits.

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.
Reply With Quote
  #6  
Old 07-31-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,656
Quote:
Originally Posted by vish_indian
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.
See: Weird problem with output from "date '+3600*%H+60*%M+%S' "
Reply With Quote
  #7  
Old 07-31-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 88
Quote:
Originally Posted by Perderabo

Thanks, that did helped.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0