Unix Shell - Bash Adding 09 or 9.1 etc

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > Bash Adding 09 or 9.1 etc





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Bash Adding 09 or 9.1 etc
Keith

2006-10-28, 7:24 am

I have a script that gives a error in BASH whenever I try to add 01-09
with 1.

X="09"
X=$((X+1))

line 42: 09: value too great for base (error token is "09")

This is caused by a date command variable and I'm trying to avoid
having to make other changes in the script.


TIA





Chris F.A. Johnson

2006-10-28, 7:24 am

On 2006-10-28, Keith wrote:
> I have a script that gives a error in BASH whenever I try to add 01-09
> with 1.
>
> X="09"
> X=$((X+1))
>
> line 42: 09: value too great for base (error token is "09")
>
> This is caused by a date command variable and I'm trying to avoid
> having to make other changes in the script.


Numbers beginning with 0 are taken as octal. With date number it is
easy to remove leading digits:

X=09
X=$(( ${X#0} + 1 ))

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Geoff Gigg

2006-10-28, 1:40 pm

Keith wrote:
> I have a script that gives a error in BASH whenever I try to add 01-09
> with 1.
>
> X="09"
> X=$((X+1))
>
> line 42: 09: value too great for base (error token is "09")
>
> This is caused by a date command variable and I'm trying to avoid
> having to make other changes in the script.


I had the same problem. Solved most easily by forcing BASH to treat the
number as decimal, instead of defaulting to octal because of leading 0.
How to force it? From BASH manpage:

Constants with a leading 0 are interpreted as octal numbers. A leading
0x or 0X denotes hexadecimal. Otherwise, numbers take the form
[base#]n, where base is a decimal number between 2 and 64 representing
the arithmetic base, and n is a number in that base.

$ x="09"
$ let y=5+x
bash: let: 09: value too great for base (error token is "09")
$ let y=5+10#x
bash: let: y=5+10#x: value too great for base (error token is "10#x")
$ let y=5+10#$x
$ echo $y
14

So you have to use the 10#$x format for your suspect variablesin your
let statements to force the base 10 caclulation.

Geoff
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com