Ads by Google

Thursday, April 10, 2008

Shell scripting gotchas: value too great for base (error token is "09")

Staring at the screen doesn't help.

Rerunning the code another 5 times doesn't help either.

It's still the same error. And it had been working so well for so many days. The code in question

if [[ $SDAY -eq $EDAY && $SMON -eq $EMON && $SYEAR -eq $EYEAR ]]; then
echo "Data is current for $value_set"
break
fi

You know you're good at shell scripting when you start making elementary mistakes and enter brain freeze territory frequently. I mean, your code can't just stop like that, can it?

And slowly you wrack your brains to see what you should be looking at.

Finally you read the error message and figure out that this is something to do with the base system.

You google and smack your head and realise, the dates were being read as octal and like clockwork it fails on 09th of April. You start to think of all the code to strip leading 0s and suitably chastened you stick to a simpler version which goes like

if [[ "$EYEAR$EMON$EDAY" -eq "$SYEAR$SMON$SDAY" ]]; then
echo "No deltas to be captured"
break
fi

The dates have to match, so it doesn't matter if it is a string or number.

Overconfidence, the bane of a programmer.