I want to know the appropriate way of reading date from terminal and comparing with current date using shell script,
I have the below script,
a=`date +%Y-%m-%d`
while [ 1 ] ; do
echo "Enter Date"
read todate
if [ $todate < $a ];then
break;
fi
echo "todate greater than curDate"
done
it is not running as expected. Please help me.
UPDATE
Here is my final version,
#! /bin/bash
DATE=$(date '+%s')
while [ 1 ] ; do
echo "Enter Date[DD MM YYYY]:"
read D M Y
THIS=$(date -d "$Y-$M-$D" '+%s')
if (( THIS < DATE )) ; then
break
fi
done
Thanks everyone!
from Advanced Bash-Scripting Guide:
7.3. Other Comparison Operators
...
string comparison
...
So, your
becomes
or
date has
+%s
format.you save current date in second. Then convert user input date also in second. so you could compare.
Here is the solution:
Here in this solution i am converting dates to single integer and it is obvious that greater date will always be larger integer than current date
a=
date +%Y%m%d
while [ 1 ] ; do
echo "Enter Date" read todate echo "$todate" > temp
done