|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|