Afficher un message
Vieux 28/10/2006, 16h18   #3
Geoff Gigg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash Adding 09 or 9.1 etc

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
  Réponse avec citation
 
Page generated in 0,04976 seconds with 9 queries