2

I am working on a shell script which will do some things if the current time is between 19:00 and 1:00 am, so I get the current time in this way:

 currenttime=$(date +%H%M%S);

and I convert the starttime and endtime format like this:

 starttime=$(date -d 19:00 '+%H%M%S');

 endtime=$(date -d 01:00 '+%H%M%S');

so I make this condition to check if the current time is between 19 and 1 AM:

num1=$(($starttime-$currenttime))
num2=$(($endtime-$currenttime))


if [ $num1 -lt 0 -a $num2 -gt 0 ];
 then
 echo "the current time is between 19:00 and 01:00 "
 else 
 echo "the current time is not in range  "
  fi

so if current time is:

20:00

the value of num1 will be:

190000 - 20000= -1000

and the value of num2 will be:

010000 - 20000= -10000

so the condition will not apply because num2 always smaller than zero. Please help. How can I make true comparison?

bentek
  • 2,235
  • 1
  • 15
  • 23
ahmad blat
  • 21
  • 1

1 Answers1

1

It's possible that you may be taking on too much at once by trying to combine both cases into a single test.

You may find it helpful to approach this problem by splitting the test into two cases:

  1. Is the time between 1900 and midnight?

  2. Is the time between midnight and 0100?

And finally, your usage of between is somewhat ambiguous. What if the time is exactly 19:00 or exactly 01:00? Is that "between" the limits or not? Using different values as an example, a strict interpretation of "between" might dictate that the only integer "between" 1 and 3 is 2. These are sometimes called "edge cases" and your problem as stated does not fully define what result you want when one of these edge cases arises.

Jim L.
  • 655
  • 4
  • 11