BASH: The octal problem

I was writing a whole pile of scripts which performed end of month tasks for this webserver, including stats archival, monthly backups, clean up bits and pieces. They all ran great until I hit the end of August and BANG…everything fell apart in a heap of “value too great for base (error token is “08”)” Damn, said I, so off to find a way to fix it all.

The problem turned out to be Octal numbering, which can be solved by lifting whole chunks of somebody much smarters post:
from Payne’s Playhouse ยป bashing octal numbers into decimal

The problem is fixed in one of two ways. GNU date can be told not to zero pad the numbers by inserting a hyphen between the ‘%’ and ‘m’:
MTH=$((`date +%-m` – 1))
The bash shell can also be told the number is decimal by using the base#number notation as follows:
MTH=$((10#`date +%m` – 1))
For September this modifies the date output to be 10#09, which bash evaluates as a decimal instead of octal 9. This method can be applied more generally to anytime a script is returning zero padded values that should be interpreted as decimal numbers.

What other people are saying...

No one has had anything to say about this post yet. Why don't you be the first?

Your two cents...