Massimo Vantaggio
Massimo Vantaggio

Reputation: 269

Bash compare two dates

Trying to figure out how to compare two dates to determinate which is major, The format is week's days, hour, minute, disposed into 3 variables.

INPUT EXAMPLE TEXT FILE WITH THE MAXIMUM VALUE

7
23
59

THE BASH

#!/bin/bash
#CHECKTIME

{
   IFS= read -r d
   IFS= read -r h
   IFS= read -r m
} < myFile.txt

echo schedule time:
echo $d
echo $h
echo $m


#GET TIME
IFS=- read -r DAY HOUR MINUTE < <(date +%u-%H-%M)
echo current time:
echo $DAY
echo $HOUR
echo $MINUTE


if [ "$DAY" >= "$d" ] && [ "$HOUR" >= "$h" ]
#if (("$DAY" = "$d"))
then
    echo "do event"

else
        echo "don't do event"
fi

Upvotes: 0

Views: 139

Answers (2)

Ed Morton
Ed Morton

Reputation: 203324

Change the input file to not include newlines between the numbers:

$ cat myfile.txt
72359

then just do this:

$ IFS= read -r foo < myfile.txt
$ bar=$(date '+%u%H%M')
$ if (( foo > bar )); then echo "go foo!"; else echo "go bar!"; fi
go foo!

The values of foo and bar above (pick better names for yourself) are:

$ echo "$foo"
72359
$ echo "$bar"
71918

If you MUST keep myfile.txt in it's current format with newlines:

$ cat myfile.txt
7
23
59

then just change the read line above to:

$ foo=$(tr -d $'\n' < myfile.txt)

and leave the rest of the script as I showed.

Upvotes: 2

Massimo Vantaggio
Massimo Vantaggio

Reputation: 269

Something like: is a wild guess.. If this script can't run before midnight with something to be scheduled before midnight, it can't compare the days equals to.. But if i use major or equal for days, the hour and minutes could un match.

#!/bin/bash
#CHECKTIME

#GET SCHEDULE TIME
{
   IFS= read -r d
   IFS= read -r h
   IFS= read -r m
} < myFile.txt

echo schedule time:
echo $d
echo $h
echo $m

SCHEDULE=$h$m
echo $SCHEDULE

#GET TIME
IFS=- read -r DAY HOUR MINUTE < <(date +%u-%H-%M)
echo current time:
echo $DAY
echo $HOUR
echo $MINUTE

TIME=$HOUR$MINUTE
echo $TIME


if [ "$DAY" == "$d" ] && [ "$TIME" -ge "$SCHEDULE" ]

then
    echo "do event"

else
        echo "don't do event"
fi

Upvotes: 0

Related Questions