miriam english (miriam_e) wrote,
miriam english
miriam_e

odd-even weeks or calculating fortnights

This calculates even weeks, that is, it can calculate fortnights:
expr \( `date +%s` / 604800 \) % 2 >/dev/null || echo "even"

or if you want odd weeks:
expr \( `date +%s` / 604800 + 1 \) % 2 >/dev/null || echo "odd"

There are 604800 seconds in a week.
"% 2" gives the remainder after division by 2.
When expr evaluates to zero the command after the OR (||) is run.

My rubbish collection is on alternate weeks. But cron doesn't know about fortnights and I only put my rubbish out once every two or three months (I don't have much waste). So I use this in cron to trigger an alert on the appropriate day:

0 10 * * mon expr \( `date +\%s` / 604800 + 1 \) \% 2 > /dev/null || alert "garbage day"

I can't remember why I escaped the "%" symbols. I think maybe cron chokes on them if you don't.

Oh, I should add that "alert" is not a standard command. It is a script that I wrote which puts a notice on the screen and uses speech synthesis to announce the message.

(Crossposted from https://miriam-e.dreamwidth.org/332340.html at my Dreamwidth account. Number of comments there so far: comment count unavailable)
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments