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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find out future date purveshvora UNIX for Dummies Questions & Answers 2 06-03-2009 02:30 AM
add or modify if existent melanie_pfefer Shell Programming and Scripting 4 03-28-2007 04:28 AM
How Do I Set a Task to Happen in the Future? Slick UNIX for Dummies Questions & Answers 2 08-09-2006 11:38 AM

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 10-07-2007
DKNUCKLES DKNUCKLES is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 12
Checking for future / non existent dates

'm attempting to script an application for the bash shell. The application needs to check for birthday, but must check the birthday to see if the date is a) in the future b) exists at all (ie Feb 29th during non-leap years). The input is being entered in a YYYYMMDD format, so I was hoping someone could point me in the direction for how to check for future dates and non-existent dates.

Thanks
- D
  #2 (permalink)  
Old 10-07-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,246
Korn:
Code:
#! /usr/bin/ksh

DATE=$1
typeset -i day

eval $(echo $DATE | sed 's/^\(....\)\(..\)\(..\)/year=\1 month=\2 day=\3/')

cal $month $year 2> /dev/null | grep -w $day > /dev/null
if [[ $? -eq 0 ]] ; then
        echo "Valid Date"
else
        echo "Invalid Date"
fi

or same thing in bash
Code:
#! /bin/bash

DATE=$1
declare -i day

eval $(echo $DATE | sed 's/^\(....\)\(..\)\(..\)/year=\1 month=\2 day=\3/')

cal $month $year 2> /dev/null | grep -w $day > /dev/null
if [[ $? -eq 0 ]] ; then
        echo "Valid Date"
else
        echo "Invalid Date"
fi

  #3 (permalink)  
Old 10-09-2007
royalibrahim royalibrahim is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 86
This script will validate the format and checks for the date availability in the calendar (thus solves leap year problem). But as the user expected it will not check whether the date falls on future or not. So the user has to take care of it in his script like this:

if [ "`date '+%Y%m%d'`" -ge "$DATE" ]; then
.....
fi
Closed Thread

Bookmarks

Tags
bash, bash eval, eval

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 04:00 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