0

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
  • [edit] your question to include concise, testable sample input and expected output so we can help you. – Ed Morton Aug 02 '20 at 23:40
  • @EdMorton DID, currently i try to compare the day of the week, if they are equals, then concat HOUR and MINUTE to get something like 2359. so determinate if the number is major.. – Massimo Vantaggio Aug 02 '20 at 23:54
  • Why are you using such an odd date+time format instead of just YYYYMMDDhhmmss or similar where you could simply do a direct string comparison? – Ed Morton Aug 02 '20 at 23:57
  • Due to make simple the user php form, and i thought to not auto fill it with php, i thought this is the simple way – Massimo Vantaggio Aug 03 '20 at 00:04
  • Idk about the php part but otherwise, the format I suggested is the simplest to work with when doing comparisons. Even if you stick with the format you have though if you store your date without newlines in the same format as you use when you call `date` then all you need is `read foo < file; bar=$(date ...); if (( foo > bar )) do stuff`. What you're trying to implement is way too complicated for the simple task at hand. – Ed Morton Aug 03 '20 at 00:08
  • This question is rather specific, but the general topic of comparing dates is better treated in https://stackoverflow.com/questions/5895159/bash-script-compare-two-date-variables – tripleee Nov 03 '21 at 06:09

2 Answers2

2

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.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • Thanks! @Ed Morton, but if the day 7 i will schedule something for the day one, it will immediately starts, this is why i try to compare the day with equal – Massimo Vantaggio Aug 03 '20 at 00:30
  • What if the user decides to have 7 week days, 245 hours, and -40 minutes, or more realistically , 7 days, 3 hours, and 5 minutes (735) and 7 days 3 hours and 55 minutes (7355) – kvantour Aug 03 '20 at 08:50
  • @kvantour the OP is getting the date using `date` and %H and %M in the date format string print hours and minutes in 2-digit format, 00..23 and 00..59 respectively. – Ed Morton Aug 03 '20 at 13:30
  • @EdMorton How do you know, it seems he only uses this mid-script but the formatting of `myFile.txt` is not given – kvantour Aug 03 '20 at 13:33
  • @kvantour 1) In his script he's comparing the date hours to the saved hours - that would make no sense if they were 2 different denominations, 2) he's reading it from the file as d+h+m - doing that would make no sense if the hours weren't relative to the given day, and 3) he says in his example `INPUT EXAMPLE TEXT FILE WITH THE MAXIMUM VALUE` and then shows `7 23 59` as that value. – Ed Morton Aug 03 '20 at 13:42
  • 1
    Oh, you're not talking about the `7 week days, 245 hours,` stuff any more, you're just talking about the numbers being zero-padded, right? I don't know if he's doing that, he'd need to. I said in [my comment](https://stackoverflow.com/questions/63221920/bash-compare-two-dates/63222212?noredirect=1#comment111795797_63221920) that he had to save the values in the same format as his date output, I just didn't carry that statement over into my answer. – Ed Morton Aug 03 '20 at 13:45
  • Indeed, I was referring to the zero padding, not the 245h thing. – kvantour Aug 03 '20 at 13:49
  • @MassimoVantaggio - OK, now I think I understand that in your application a current day number of `1` is actually greater than a stored day number of `7` because, for example, you might be running your tool on a Sunday (day 7) to schedule something to start at some other time and if that time is on Monday (day 1) then you need to consider that the future rather than the past. Is that right? If so, it's doable but much easier, more robust (if you run on Sun to schedule for Sun do you mean today or next Sun?) and less confusing if you just use a date format like YYYYMMDDhhmmss - can you do that? – Ed Morton Aug 03 '20 at 13:52
  • 1
    @kvantour got it. I'm going to have to rework this answer anyway now I have a better idea (I think!) of what the OP is trying to do once they reply about whether they can use a sensible timestamp format or not. – Ed Morton Aug 03 '20 at 13:53
0

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