08: value too great for base (error token is "08")的shell进制问题

原帖地址:http://blog.sina.com.cn/s/blog_699f1fcc01017e1b.html


今天在执行之前写的获得上月月份的脚本时突然报错:08: value too great for base (error token is "08"),因为之前写脚本时并没有测试8月份,只简单测试了8以下和12月的了,导致了隐患的存在。
于是上网查找原因,找到如下内容:原文地址:http://hi.baidu.com/kingdouble8008/blog/item/971ed21dce29f69686d6b675

.html
原因:以下是代码片段:
Numbers starting with leading 0 are Octal numbers (base 8) in many programming
languages including C, Perl and shell. Valid octal digits are
0,1,2,3,4,5,6,7 so it barfs if it sees an 8 or a 9. You probably want
to work in straight numbers and make a leading 0 in your output
format with a sprintf("d") kind of formatting thing.
Anything starting with 0x or 0X is a hex number.

So the error message means exactly as it says- it's an error from
the let function complaining about the value being too big for the base.

上面就是说以0开头的被默认为八进制了,所以08就是无效的八进制数喽,呵呵

解决方案:以下是代码片段:
You can explicitly state the base of a number using base#number
Code:
if [ $((10#$item)) -eq 0 ] ; then
That will have trouble if the number starts with a minus sign.
The '-' needs to be in front of the base like -10#009 for -9.

上面就是说解决方法可以用 进制#变量 来转换,如08 改成10进制 10#08 或 10#$var ,
还有就是关于负号 - 要放在前面 -10#08


你可能感兴趣的:(shell)