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

Bash

Bash Problem Overview


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?

Bash Solutions


Solution 1 - Bash

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.

Solution 2 - Bash

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

Solution 3 - Bash

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. For this example I'm assuming you're running this command during the 09:00 hour.

Without prefixing date format with hyphen:

$ if (( $(date +%H) < 10 )); then echo true; else echo false; fi
-bash: ((: 09: value too great for base (error token is "09")
false

With prefixing date format with hyphen:

$ if (( $(date +%-H) < 10 )); then echo true; else echo false; fi
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

Solution 4 - Bash

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

and then

((diff_sec=d2-d1))

What do you expect to get? ((diffsec=2014-01-09-2014-01-10)) ??

You need to convert the dates to seconds first:

d1=$( date -d "${filedates[$t]}" +%s )
d2=$( date -d "${filedates[$t+1]}" +%s )
(( compare = (d2 - d1) / (60*60*24) ))
(( value += compare ))

Solution 5 - Bash

You don't need the $ and the {} in an arithmetic expansion expression. It should look like this:

compare=$((SEC/(60*60*24)))

Solution 6 - Bash

For 'mm' and 'dd' values in dates, I use this trick:

mm="1${date:5,2}"  # where 5 is the offset to mm in the date
let mm=$mm-100     # turn 108 into 8, and 109 into 9

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3178889View Question on Stackoverflow
Solution 1 - BashrojomokeView Answer on Stackoverflow
Solution 2 - BashdogbaneView Answer on Stackoverflow
Solution 3 - BashenharmonicView Answer on Stackoverflow
Solution 4 - Bashglenn jackmanView Answer on Stackoverflow
Solution 5 - Bashhek2mglView Answer on Stackoverflow
Solution 6 - BashDick GuertinView Answer on Stackoverflow