PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Bash Adding 09 or 9.1 etc
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Bash Adding 09 or 9.1 etc

Réponse
 
LinkBack Outils de la discussion
Vieux 28/10/2006, 08h58   #1
Keith
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Bash Adding 09 or 9.1 etc

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





  Réponse avec citation
Vieux 28/10/2006, 10h26   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash Adding 09 or 9.1 etc

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
  Réponse avec citation
Vieux 28/10/2006, 15h18   #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
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h45.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09726 seconds with 11 queries