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?
You don't need the
$
and the{}
in an arithmetic expansion expression. It should look like this: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.
and then
What do you expect to get?
((diffsec=2014-01-09-2014-01-10))
??You need to convert the dates to seconds first:
The fowllowing code occur the same error , I don't know why, but already solved it:
ERROR INFO:
Fix:
What are
d1
andd2
? 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:
In order to perform arithmetic with 0-prefixed numbers you need to tell bash to use base-10 by specifying
10#
: