The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 07:39 AM
Specify a previous date as start date in shell script ritzwan0 Shell Programming and Scripting 2 09-25-2006 06:58 PM
date calculation program axes High Level Programming 1 09-12-2006 11:11 PM
script execution time calculation johnsonbryce Shell Programming and Scripting 9 02-25-2006 01:33 AM
Math calculation help sickboy Shell Programming and Scripting 4 06-11-2005 02:22 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-31-2006
vish_indian vish_indian is offline
Registered User
  
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 92
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 09:25 AM.. Reason: Added code tags
  #2 (permalink)  
Old 07-31-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 579
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 (permalink)  
Old 07-31-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
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.
  #4 (permalink)  
Old 07-31-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,122
I have linked this thread into the faq article. It's always good to have another alternative.
  #5 (permalink)  
Old 07-31-2006
vish_indian vish_indian is offline
Registered User
  
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 92
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.
  #6 (permalink)  
Old 07-31-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,122
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' "
  #7 (permalink)  
Old 08-01-2006
vish_indian vish_indian is offline
Registered User
  
 

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

Thanks, that did helped.
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0