0

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

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by jim mcnamara
0

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.

0

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

Edited by diafol: fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.