5

My date format is yyyy-mm-dd-hh:mm:ss How do I check my input?

It should be something like this:

#!/bin/bash

read -p "Date (format yy-mm-dd-HH-MM-SS): " input

check=$(date +"%Y-%m-%d-%H:%M:%S")

if [ $input -eq $check ]; do

     echo "Right!"

else
     echo "False!"

fi

But that doesn't check the date It compares my input with the real date.

Best Regards Vince

Majid Laissi
  • 19,188
  • 19
  • 68
  • 105
Vince
  • 1,133
  • 3
  • 17
  • 34

3 Answers3

9

Edited apr 2016!

See further (stronger method)

Original post

Try:

#!/bin/bash

read -p "Date (format yyyy-mm-dd): " input
check=$(date +%F)

if [ "$input" == "$check" ]; then
    echo "Right!"
else
    echo "False!"
fi

or

#!/bin/bash

read -p "Date (format YYYY-MN-DD-HH24:MM:SS): " input
check=$(date +%F-%T)

if [ "$input" == "$check" ]; then
    echo "Right!"
else
    echo "False!"
fi

Well tested:

cat >hesdate.sh     # Copy 1st sample and paste to terminal
chmod +x hesdate.sh
date +%F ; ./hesdate.sh
2013-01-04
Date (format yyyy-mm-dd): 2013-01-04
Right!

cat >hesdate.sh     # Copy 2nd sample and paste to terminal
date -d now\ +10\ sec +%F-%T ; ./hesdate.sh 
2013-01-04-10:17:06                                       # copy this line
Date (format YYYY-MN-DD-HH24:MM:SS): 2013-01-04-10:17:06  # past exactly 10 secs after
Right!

Edit add

For testing a date, you could:

[[ $input =~ ^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$ ]]

if [[ $input =~ ^2012-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$ ]];then

and/or

inputSecs=$(date -d "${input%-*} ${input##*-}" +%s)

Using boot method let you confirm format and reliability of input

Stronger method

If you want to check input, there is a finer method:

unset adate
declare -A adate
date=2013-12-04-10:17:06
for field in s:0-59 m:0-59 h-0-23 D-1-31 M-1-12 Y#2000-2100 ;do
   sep=${field:1:1} min=${field:2} field=${field:0:1} max=${min#*-} min=${min%-*}
   crt=${date##*${sep:-#}}
   ((min<=10#$crt&&10#$crt<=max)) && adate[$field]=$crt ||
       echo Error: $crt not between $min and $max in $field field.
   date=${date%$sep*}
 done
declare -p adate

This will dump adate array variable:

declare -A adate='([D]="04" [M]="12" [Y]="2013" [h]="10" [m]="17" [s]="06" )'

From there, you could re-validate day number:

max=$(date -d "${adate[Y]}-${adate[M]}-1 +1 month -1 day" +%d)
((10#${adate[D]}>max)) && echo "Error Day number too high: (${adate[D]}>$max)."

The only thing not tested there is field length if

date=2012-02-29-10:17:06

will work, then

date=2012-2-29-10:17:06

will work too (there is only one digit in day field).

If needed, you could change the line:

for field in s:0-59 m:0-59 h-0-23 D-1-31 M-1-12 Y#2000-2100 ;do
sep=${field:1:1} min=${field:2} field=${field:0:1} max=${min#*-} min=${min%-*}
crt=${date##*${sep:-#}}

for

for field in s:20-59 m:20-59 h-20-23 D-21-31 M-21-12 Y#42000-2100 ;do
sep=${field:1:1} len=${field:2:1} min=${field:3} field=${field:0:1} max=${min#*-} min=${min%-*}
crt=${date##*${sep:-#}}
[ ${#crt} -eq $len ] || echo "Error: Field $field is no $len len: ${#crt}."

Nota: Year field is arbitrarily limited between 2000 and 2100, but this is easy to understand/change.

techno
  • 525
  • 3
  • 13
  • Hi techno, Thats right. But i dont want to compare it with the real date. I only want to check, if the format of the date i typed in is right. – Vince Jan 04 '13 at 09:30
  • For Example: I type in: 2012-12-20-10-00-00 --> the program give me an error message. When I type: 2012-12-20-10:00:00 --> the programm gives me an OK – Vince Jan 04 '13 at 09:37
  • Look my adds on my answer! – techno Jan 04 '13 at 09:39
  • That is it, what I'm searching for. Thank you! :D Could you please explain me, what the if query is exactly doing? – Vince Jan 04 '13 at 09:46
1

You can use an inline AWK script:

#!/bin/bash

read -p "Date (format yy-mm-dd-HH-MM-SS): " input

echo $input | awk -F"[-:]" '{
    if( NF == 6 && $1 >= 0 && $2 >= 1 && $2 <= 12 && $3 >= 1 && $3 <= 31 && $4 >= 1 && $4 <= 24 && $5 >= 1 && $5 <= 60 && $6 >= 1 && $6 <= 60 ){
        print "Ok!\n";
    } else {
        print "False!\n";
    }
}'
JTextor
  • 71
  • 3
0

Change the date prompt like this:

read -p "Date (format yyyy-mm-dd): " input

Also, format the date with 4 digit year, and seconds since the minute with

check=$(date +"%Y-%m-%d %H:%M:%S")
wallyk
  • 56,922
  • 16
  • 83
  • 148
  • Okay, I've edited it now. But i want to check my date with this format. My Program compares my input with the real date.. – Vince Jan 04 '13 at 08:00