bash 基础值太大(错误标记为“09”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21049822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Value too great for base (error token is "09")
提问by user3178889
When running this part of my bash script am getting an error
运行我的 bash 脚本的这一部分时出现错误
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
d1 和 d2 是 2014-01-09 和 2014-01-10 形式的日期
Any solution please?
请问有什么解决办法吗?
回答by rojomoke
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.
在变量前面添加字符串“10#”。这迫使 bash 将它们视为十进制,即使前导零通常会使它们成为八进制。
回答by dogbane
What are d1
and d2
? Are they dates or seconds?
什么是d1
和d2
?它们是日期还是秒?
Generally, this error occurs if you are trying to do arithmetic with numbers containing a zero-prefix e.g. 09.
通常,如果您尝试对包含零前缀的数字(例如 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#
:
为了使用以 0 为前缀的数字执行算术,您需要通过指定来告诉 bash 使用 base-10 10#
:
$ echo $((10#09+1))
10
回答by glenn Hymanman
d1 and d2 are dates in that form 2014-01-09 and 2014-01-10
d1 和 d2 是 2014-01-09 和 2014-01-10 形式的日期
and then
进而
((diff_sec=d2-d1))
What do you expect to get? ((diffsec=2014-01-09-2014-01-10))
??
你期望得到什么?((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 ))
回答by Wentao Ma
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" .
回答by Dick Guertin
For 'mm' and 'dd' values in dates, I use this trick:
对于日期中的 'mm' 和 'dd' 值,我使用这个技巧:
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
回答by hek2mgl
You don't need the $
and the {}
in an arithmetic expansionexpression. It should look like this:
在算术扩展表达式中不需要 the$
和 the 。它应该是这样的:{}
compare=$((SEC/(60*60*24)))