0

I need a linux shell to perform a different task on each day for 4 rotating days:

Day 1: task A
Day 2: task B
Day 3: task C
Day 4: task D

And on Day 5 start again with task A and so on forever...

So I decided to use the day of the year, do a modulo 4 (division by 4 and only keep the rest) and check vs the modulo:

#!/bin/bash
let DATETEST=$(date +%j)%4

if (($DATETEST == "0" ));
then
    do task A
fi

if (($DATETEST == "1" ));
then
    do task B...

This worked OK until 2 days ago when I started getting the error:

let: DATETEST=009: value too great for base (error token is "009")

not sure what's wrong.

Yes: Fravadona's answer solves my problem. I also found the following note which helps and explains base10: https://stackoverflow.com/a/5455805/1829159

Chepner's comment also works for me and allowed me to discover that LET was obsolete...

Blaise
  • 143
  • 1
  • 1
  • 9
  • 2
    `let "DATETEST = 10#$(date +%j) % 4"` – Fravadona Jan 09 '22 at 22:19
  • I think when you put `0` at the beginning of the number it is interpreting it in octal. In octal, `9` would be too big. – xdhmoore Jan 09 '22 at 22:20
  • 2
    type `man 5 crontab` in a shell. – Keith Jan 09 '22 at 22:22
  • 1
    `let` is obsolete. `DATETEST=$(( 10#$(date +%j) % 4 ))`. – chepner Jan 09 '22 at 22:28
  • Out of curiosity, where do you live that it was the 9th of the year *two* days ago? – chepner Jan 09 '22 at 22:31
  • @chepner I'm guessing OP was referring to the `value too great for base` part of the message as having started 2 days ago; it's currently the 10th for the western pacific and most (all?) of Asia, and 2 days ago would've been the 8th, which would've also generated the error message (ie, `008` is not a valid octal value); perhaps the job has not run yet for the `10th` so OP has cut-n-pasted the last/latest error message? then again, doesn't everyone have a VM running a few hours/days 'in the future'? :-) – markp-fuso Jan 09 '22 at 22:57
  • Oh, right, overlooked that 008 was also a problem. D'oh! – chepner Jan 09 '22 at 23:05

0 Answers0