飞翔灬吾爱的Blog
Linux | shell脚本value too great for base
2019-5-24 fishyoung

今天在编写shell脚本的时候发现了这个问题:

08: value too great for base (error token is "08")

 

原因是:

tar_file_month=`date +%m`

rsync -auzq --exclude="2018_0[1-9]" --exclude="2018_1[0-2]" --exclude="2018_0[1-"$(($tar_file_month-1))"]" 

当月是8月,tar_file_month变成了08,下面执行  $(($tar_file_month-1))的时候,变成了08-1,而0x对于shell来说,会解析成8进制,08超过了8进制的表示范围,所以报错:shell value too great for base;

解决方法:[hide]告诉shell使用10进制,加个10#

10#$tar_file_month-1

即代码更正为:

tar_file_month=`date +%m`

rsync -auzq --exclude="2018_0[1-9]" --exclude="2018_1[0-2]" --exclude="2018_0[1-"$((10#$tar_file_month-1))"]"[/hide]