Home » Infrastructure » Unix » Date Validation
Date Validation [message #117876] Sat, 30 April 2005 09:44 Go to next message
mrinalkumar01
Messages: 6
Registered: April 2005
Junior Member
I have a shell script which has date as on of the input parameters.
whenever i pass on a date as parameter to this script i need to validate whther the date that has been passed is valid or not.
Till now i could not find command or a utility in Unix to do this.

Is there any inbuilt utility in Unix by which we can check whether a date passed is valid or not.
Has any one else faced this problem and if so, how was the problem solved.
Thanks in Advance.
Re: Date Validation [message #118048 is a reply to message #117876] Mon, 02 May 2005 18:00 Go to previous messageGo to next message
andrew again
Messages: 2577
Registered: March 2000
Senior Member
If you are calling sqlplus in that script already, then I would just validate it in oracle using to_date(). If the concversion fails, just return an error code to Unix and handle it in your shell script. I'd do it in Oracle becuase it's easy for me to code. http://www.oracle.com/oramag/oracle/01-may/o31asktom.html
Re: Date Validation [message #118085 is a reply to message #117876] Tue, 03 May 2005 00:08 Go to previous messageGo to next message
mrinalkumar01
Messages: 6
Registered: April 2005
Junior Member
Hi Andrew,
The problem that i am facing is that i am not calling sqlplus in this script. so i have to validate the date using Unix Script only.
Please let me know if there is a way out or i should use multiple If's to validate the date.
Re: Date Validation [message #118244 is a reply to message #118085] Tue, 03 May 2005 16:34 Go to previous messageGo to next message
andrew again
Messages: 2577
Registered: March 2000
Senior Member
http://www.intuitive.com/wicked/showscript.cgi?007-valid-date.sh

Try a google search on "shell date validate"
Re: Date Validation [message #347954 is a reply to message #117876] Mon, 15 September 2008 03:17 Go to previous message
marcohensel
Messages: 1
Registered: September 2008
Location: germany
Junior Member
or try this little script Smile

-----------------------------------------------


#!/usr/local/bin/zsh
# tested on FreeBSD 6.2
# max. logical date: 203801181201
# simple to use, example -> # validate.sh 19790231

date=$1

if [[ ! "$date" = ([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) ]]
then
echo "input incorrect, \"ccyymmdd\""
exit 1
fi

if [ "$date" -gt "20380118" ]
then
echo "date invalid"
exit 1
else
testdate=`date -j "$date"1201 "+%Y%m%d"`
if [ "$testdate" -eq "$date" ]
then
exit 0
else
echo "date invalid"
exit 1
fi
fi
Previous Topic: unix issue
Next Topic: doubt in grep .
Goto Forum:
  


Current Time: Thu Mar 28 19:02:51 CDT 2024