lumpa
lumpa

Reputation: 49

evaluate next date and if the first friday of month echo that for me

My try:

we=$(LC_TIME=C date +%A)
dm=$(date +%d)
wday="Friday"

            if [ "$we" = "$wday" ] && [ "$dm" -lt 8 ]
            then do some stuff

I means on first friday he make sume stuff. How can I store this in a variable, that if the next time is the first Friday in the month for an echo output.

Now the script executes me only on the first Friday in the month something.

It must be evaluate if the next friday is in the next month or not if yes do some. (Debian 11)

Upvotes: 1

Views: 284

Answers (1)

tshiono
tshiono

Reputation: 22012

As your OS is debian, I assume GNU date command which supports -d option is available. Then would you please try the following:

dayofweek="$(date -d "tomorrow" +%u)"  # day of week (1..7); 1 is Monday
dayofmonth="$(date -d "tomorrow" +%d)" # day of month (e.g., 01)
if (( dayofweek == 5 && 10#$dayofmonth < 8 )); then
    # do some stuff
fi
  • As the format %u prints the day of week between 1 and 7, you can compare it with 5 to detect Friday.
  • The day of month includes the leading zero such as 01 and you need to convert it to a decimal digit number by prefixing 10#. Otherwise the number with a leading zero is interpreted as octal then 08 and 09 will cause an error.

Upvotes: 1

Related Questions