Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value too great for base (error token is "09")

Tags:

bash

When running this part of my bash script am getting an error

Script

value=0
for (( t=0; t <= 4; t++ ))
do
d1=${filedates[$t]}
d2=${filedates[$t+1]}
((diff_sec=d2-d1))
SEC=$diff_sec
compare=$((${SEC}/(60*60*24)))
value=$((value+compare))
done

Output

jad.sh: line 28: ((: 10#2014-01-09: value too great for base (error token is "09")
jad.sh: line 30: /(60*60*24): syntax error: operand expected (error token is "/(60*60*24)")

d1 and d2 are dates in that form 2014-01-09 and 2014-01-10

Any solution please?

like image 919
user3178889 Avatar asked Jan 10 '14 16:01

user3178889


3 Answers

Prepend the string "10#" to the front of your variables. That forces bash to treat them as decimal, even though the leading zero would normally make them octal.

like image 91
rojomoke Avatar answered Nov 03 '22 17:11

rojomoke


What are d1 and d2? Are they dates or seconds?

Generally, this error occurs if you are trying to do arithmetic with numbers containing a zero-prefix e.g. 09.

Example:

$ echo $((09+1))
-bash: 09: value too great for base (error token is "09")

In order to perform arithmetic with 0-prefixed numbers you need to tell bash to use base-10 by specifying 10#:

$ echo $((10#09+1))
10
like image 19
dogbane Avatar answered Nov 03 '22 15:11

dogbane


As others have said, the error results from Bash interpreting digit sequences with leading zeros as octal numbers. If you have control over the process creating the date values and you're using date, you can prefix the output format string with a hyphen to remove leading zero padding.

Without prefixing date format with hyphen:

$ (( $(date --date='9:00' +%H) > 10 )) && echo true || echo oops
-bash: ((: 09: value too great for base (error token is "09")
oops

With prefixing date format with hyphen:

$ (( $(date --date='9:00' +%-H) > 10 )) && echo true || echo oops
true

From the date man page:

By default, date pads numeric fields with zeroes. The following optional flags may follow '%':

  -      (hyphen) do not pad the field
like image 7
enharmonic Avatar answered Nov 03 '22 16:11

enharmonic