In a bash script, the number of the current week is used to select a different tape every week (rotation of 4 tapes) :
#!/bin/bash WEEK=`date +%W` # Week number (01..52) TAPE=$((WEEK % 4))
This worked fine so far, but this weekend (it's the 8th weekend of the year) there was an error :
value too great for base (error token is "08")
After some digging, I found that bash treats numbers with a leading 0 as octal. But value 8 (and 9) don't exist in octal, so that's why it throws an error. With smaller numbers (01-07), there is no problem, because they are valid octal numbers, and for bigger numbers (10-...), the number is treated in decimal format, because there is no leading 0 anymore.
This problem can be fixed in two ways :
1) strip leading 0 from value
In bash you can do this with this command :
WEEK=${WEEK#0} # strip leading 0
This only removes a leading zero, a zero that is not leading will not be removed.
2) convert value to decimal
TAPE=$((10#$WEEK % 4)) # convert $WEEK to base-10 with '10#'
Note that you have to add the $ in front of the variable WEEK, to treat it is a variable, in order for the conversion (with 10#) to work.
1 comment:
Try out
date +%-W
The "-" strips of the leading zeho.
Ex:
$ date +%m
09
$ date +%-m
9
Post a Comment