0

I'm trying to get specific dates and times to get three different outcomes from the code below. Sometimes doesn't work correctly so it has been done probably incorrectly.

#!/bin/bash
currentdate=6
currenttime=17
if [[ ${currentdate#0} -ge  5 ]] && [[ ${currenttime#0} -ge 18 ]] && [[ ${currentdate#0} -le 7 ]] && [[ ${currenttime#0} -le 22 ]]
then
echo "command for Friday after 18pm until Sunday 22pm"
elif [[ ${currentdate#0} -eq 3 ]] && [[ ${currenttime#0} -ge 18 ]] && [[ ${currenttime#0} -le 20 ]]
then
echo "command for Wednesday after 18pm until 20pm"
else
echo "command for all other dates and times"
fi

There was a logic error on my function. Worked by changing it to:

if [[ ${currentdate#0} -ge  5 ]] && [[ ${currenttime#0} -ge 18 ]] || [[ ${currentdate#0} -eq  6 ]] || [[ ${currentdate#0} -le 7 ]] && [[ ${currenttime#0} -le 22 ]]
  • This will fail whenever the hour is 08 or 09 for the reason described in the duplicate. If this is not the problem you're seeing, I'll be happy to reopen the question. Good luck, and remember to always capture debug logs! – that other guy Mar 28 '19 at 16:42
  • That was correct! I've got rid of that error on specific hours. However I still don't get the correct results if the hour is lower than 10. – Perseus Arkouda Mar 28 '19 at 17:31
  • Can you please edit your code into a [MCVE](https://stackoverflow.com/help/mcve) that people can run on their own machine (at any time) to see the problem? For example, you can change `currentdate=$(date +%u)` to `currentdate=3` and similar for currenttime, and then set the commands to something valid like `echo "Unexpected"` and `echo "Expected"`. Then add a note about what you expected to happen, and what happens instead – that other guy Mar 28 '19 at 17:40
  • Actually found what my problem was. There is a logic error on my function. I wanted to get a period from Friday after 18pm until Sunday 22pm. So I'm getting wrong results in that period if the hour is lower than 18. Any suggestions? – Perseus Arkouda Mar 28 '19 at 17:45
  • Rewrite it to "Friday after 18 or Saturday any time or Sunday before 22" – that other guy Mar 28 '19 at 17:48
  • @PerseusArkouda Please [edit] your question to add this clarification and maybe a full description when you want to run the three commands similar to your comment "from Friday after 18pm until Sunday 22pm" – Bodo Mar 28 '19 at 17:49
  • @thatotherguy thank you very much! Works like a charm now! – Perseus Arkouda Mar 28 '19 at 17:54

1 Answers1

0

You could combine the day number and hour to one number and compare it as a whole. With this approach you can define any time range as you like.

Using one date command only instead of two has the advantage of avoiding possible wrong results when the first one is called at 23:59 and the second one at 0:00 the next day.

The hour printed with format %H has two digits (00..23), so you will always get a number of 3 digits. It doesn't matter that the hour is counted only up to 23. The current day with hour 23 will be less than the next day with hour 00. The day of week format %u prints a number in the range (1..7), so the resulting number will not have a leading 0.

daytime=$(date +%u%H)

if [ "${daytime}" -ge  518 ] && [ "${daytime}" -le 722 ]
then
    echo "command for Friday after 18pm until Sunday 22pm"
elif [ "${daytime}" -ge 318 ] && [ "${daytime}" -le 320 ]
then
    echo "command for Wednesday after 18pm until 20pm"
else
    echo "command for all other dates and times"
fi

Note: The script is based on the original script from the question. I didn't change everything that could be improved.
Instead of "${daytime}" you can also use "$daytime", $daytime or ${daytime} in this specific case. The quotes would help to get a better error message in case there was an error with the assignment of the date result to daytime resulting in a wrong or empty value of daytime The date command in this script will always print a 3-digit number without spaces, so the script works without quotes.
Some people like to use braces with all variable expansions, some don't, so it's a matter of taste here.

Bodo
  • 9,287
  • 1
  • 13
  • 29
  • If you're going to use bash-only syntax, consider `if (( daytime >= 518 && daytime <= 722 ))`. Just as nonportable, but a whole lot more readable. :) – Charles Duffy Mar 28 '19 at 18:03
  • (And if you're going to add extra characters, `"$daytime"` actually adds correctness benefits over `$daytime`, in that it still works right when IFS contains numbers; whereas `${daytime}` only adds value when doing string concatenation with a string starting with a valid variable name, which you're not). – Charles Duffy Mar 28 '19 at 18:05