Shell脚本错误:值太大,基地(错误标记是" 08") [英] Shell Script Error: Value too great for base (error token is "08")

查看:109
本文介绍了Shell脚本错误:值太大,基地(错误标记是" 08")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我的问题是要找到使用单支架[]和双括号的区别[[]]中的if语句。

 #!/斌/庆典
翻= 08;
varb = 10;##单支架中,如果说明书工作。
如果[$翻-lt $ varb]。然后
回声是;
其他
回声无;
科幻
##如果在说明书不工作双括号;抛出一个错误,如下面。
## [[:08:​​值太大,基地(错误标记为08)
如果[[$翻-lt $ varb]];然后
回声是;
其他
回声无;
科幻


解决方案

外壳尝试间$ P $ 08 PT作为一个八进制数,因为它以零开始的。只有数字0-7,然而,允许在八进制,十进制8的八进制010。因此08是不是有效的数字,这是该错误的原因。

单括号内是一种兼容模式与SH和SH不知道八进制数。

所以,如果你使用单括号,010将PTED 10间$ P $,而双括号,010将PTED 8间$ P $。

如果使用单一方括号中,08,将PTED 8间$ P $,同时用双方括号中,它不是一个有效的数,并导致错误。

您可以通过使用这里描述的解决方案避免了错误:<一href=\"http://stackoverflow.com/a/12821845/1419315\">http://stackoverflow.com/a/12821845/1419315

 如果[[$ {翻#0} -lt $ {varb#0}]

 如果[[$((10#$翻))-lt $((10#$ varb))]

Here my problem is to find the difference of using single bracket [ ] and double brackets [[ ]] in if statement.

#!/bin/bash
vara=08;
varb=10;

## single bracket in if statment is working.
if [ $vara -lt $varb ]; then
echo "yes";
else
echo "no";
fi


## double brackets in if statment is not working; throwing an error like below.
## [[: 08: value too great for base (error token is "08")
if [[ $vara -lt $varb ]]; then
echo "yes";
else
echo "no";
fi

解决方案

The shell tries to interpret 08 as an octal number, as it starts with a zero. Only digits 0-7 are, however, allowed in octal, as decimal 8 is octal 010. Hence 08 is not a valid number, and that's the reason for the error.

Single brackets are kind of "compatibility mode" with sh, and sh does not know about octal numbers.

So, if you use single square brackets, "010" will be interpreted as 10, while with double square brackets, "010" will be interpreted as 8.

If you use single square brackets, "08" will be interpreted as 8, while with double square brackets, it is not a valid number and leads to an error.

You can avoid the error by using the solution described here: http://stackoverflow.com/a/12821845/1419315

if [[ ${vara#0} -lt ${varb#0} ]]

or

if [[ $((10#$vara)) -lt $((10#$varb)) ]]

这篇关于Shell脚本错误:值太大,基地(错误标记是&QUOT; 08&QUOT;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆