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?