49
votes

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?

6
You can't just subtract dates in the form YYYY-MM-DD. You have to convert them to plain numbers first, like time_t timestamps (which will get you seconds). - Mark Reed
Looks like it's converting your 09 to octal notation, so chances are it's actually trying to compute 2014 - 1 - 9, but since 09 is not a valid number (the 0 at the front means use octal instead of decimal) it's complaining. - robbrit
what the solution robbirt? - user3178889

6 Answers

67
votes

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.

17
votes

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
3
votes

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 ))
3
votes

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
0
votes

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

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

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