943,954 Members | Top Members by Rank

Ad:
Jan 4th, 2008
0

Date Validation again

Expand Post »
I want a sample Date validation script using if loops.
The script should first compare "year".If the year is lesser than the current year,It should go for "month" checking.
I have the script that splits the date into year,month and date.I want only the checking part.
My if loop checking is not work properly.
Can you show the right way of checking?

Regards
Dave
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dave_nithis is offline Offline
23 posts
since Oct 2007
Jan 4th, 2008
0

Re: Date Validation again

Show what you have, and we will help you correct it.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jan 7th, 2008
0

Re: Date Validation again

This is the splitting part am having:

C_Date=`date -u +%m/%d/%Y`

year="$(echo $Date | cut -d/ -f3)"
month="$(echo $Date | cut -d/ -f1)"
day="$(echo $Date | cut -d/ -f2)"

cyear="$(echo $C_Date | cut -d/ -f3)"
cmonth="$(echo $C_Date | cut -d/ -f1)" cday="$(echo $C_Date | cut -d/ -f2)"
C_Date is current date i.e Today's Date.

#Validation
if [ $year -gt $cyear ]; then
echo "year out of range"
fi
Similarly am checking for month and day.Is there any other simple way to do the comparison using case Statement?


Regards
Dave.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dave_nithis is offline Offline
23 posts
since Oct 2007
Jan 10th, 2008
0

Re: Date Validation again

date -u causes UTC date/time to be displayed - FYI.

If you format date like this: [code]
today=$(date +%Y%m%d)
you get a value like 20080111, this is a number - an integer.

When you split the input reformat it:
Shell Scripting Syntax (Toggle Plain Text)
  1. newdate="$cyear$cmonth$cday"
then compare integers:
Shell Scripting Syntax (Toggle Plain Text)
  1. if [[ $today -ne $newdate ]]
  2. then
  3. echo "bad date"
  4. fi
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Need help in setting up as script..
Next Thread in Shell Scripting Forum Timeline: Begginer with Linux here nid help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC