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.

3 comments:

Anonymous said...

Hi,
It would help to probably try this:
month=${month##0}

This one cuts the leading 0 of each
value.

Anonymous said...

or use ksh to avoid the issue

The Hug Collector said...

It is irrelevant to what kind of shell it is. The shell is thinking that your number is an octal (base 8) because it is prefixed with a 0.

Just made sure it knows the number is 10 based. Say you used to define your start date by

SDAY=$(date +'%d')

Do this instead:

SDAY=10#$(date +'%d')

That would take care of your problem.