bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: value too great for base (error token is "0008")


From: Chris F.A. Johnson
Subject: Re: value too great for base (error token is "0008")
Date: Thu, 5 Nov 2009 10:25:14 -0500 (EST)
User-agent: Alpine 2.00 (LMD 1167 2008-08-23)

On Thu, 5 Nov 2009, Greg Wooledge wrote:

> On Wed, Nov 04, 2009 at 11:26:56PM +0100, Dobromir Romankiewicz wrote:
> > Thank you for your answers, I'll check more carefully before I'll send next 
> > bug report - if I'll find it, of course.
> > That was silly. For my excuse I just would like to say I'm not coder.
> 
> Heh.  I *am* a coder, and I've been tripped up by leading zeroes causing
> a value to be treated as octal in more than one language.  (Bash and Tcl.)
> It's a subtle and difficult thing to guard against, in these typeless
> or weakly-typed scripting languages.
> 
> > The point here was that 2 kinds of variables were needed. Integer number as 
> > while counter, and 4 digits string, as a part of filenames - i had add 
> > leading zeros to get this working with filesystem I can't change or even 
> > list.
> 
> The general approach here is that whenever you're dealing with something
> as a number, you must not have any leading zeroes on it; or you must
> force bash to treat it as base 10, with the "10#" prefix.  Then, when
> you're all done with the arithmetic and you're ready to put the number
> in a filename, use printf to pad it to however wide you need.  Thus,
> 
> n=10
> n=$(($n * 3 + 7)) # whatever
> filename=app$(printf %05d $n).log

   Faster is:

printf -v filename "app%05d.log" "$n"

-- 
   Chris F.A. Johnson, webmaster         <http://woodbine-gerrard.com>
   ===================================================================
   Author:
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]