Value too great for base (error token is “09”)

2020-01-25 01:30发布

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
5条回答
家丑人穷心不美
2楼-- · 2020-01-25 01:47

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

compare=$((SEC/(60*60*24)))
查看更多
虎瘦雄心在
3楼-- · 2020-01-25 01:56

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.

查看更多
Root(大扎)
4楼-- · 2020-01-25 02:07

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 ))
查看更多
forever°为你锁心
5楼-- · 2020-01-25 02:07

The fowllowing code occur the same error , I don't know why, but already solved it:

24    echo $currentVersion
25    if [[ $currentVersion -eq "" ]];then
26       echo "$projectName=$version">>$modulepath
27    else
28       sed  -i "s/^$projectName=$currentVersion/$projectName=$version/g"  $modulepath
29    fi

ERROR INFO:

b26044fb99c28613de9903db3a50cbb11f0de9c7 1e5d11c9923045cc43f5fdde07f186b6dd5ca1b4
/data/ext/tbds_ci_build/tbds_build_common.sh: line 25: [[: b26044fb99c28613de9903db3a50cbb11f0de9c7
1e5d11c9923045cc43f5fdde07f186b6dd5ca1b4: value too great for base (error token is "1e5d11c9923045cc43f5fdde07f186b6dd5ca1b4")
sed: -e expression #1, char 63: unterminated `s' command

Fix:

Make $currentVersion do not contain 2 values like "a b", just 1 value "a" .
查看更多
唯我独甜
6楼-- · 2020-01-25 02:11

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
查看更多
登录 后发表回答