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

Recommended Answers

All 3 Replies

Show what you have, and we will help you correct it.

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.

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

If you format date like this:

today=$(date +%Y%m%d)

you get a value like 20080111, this is a number - an integer.

When you split the input reformat it:

newdate="$cyear$cmonth$cday"

then compare integers:

if [[ $today -ne  $newdate ]] 
then
       echo "bad date"
fi
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.