【发布时间】:2021-10-11 23:34:05
【问题描述】:
我不明白这里发生了什么,基于Convert string into integer in bash script - "Leading Zero" number error,我能够将字符串转换为整数。我什至可以做加法。但是,当我比较数字时,它给了我错误的比较。参考下面的例子
这是一个例子
array=(02 08 10 11 23 52)
for i in 0 1 2 3 4 5
do
if [[ ${array[i]#0} > 15 ]]
then
echo "${array[i]#0} is greater than 15"
else
echo "${array[i]#0} is less than 15"
fi
done
输出
2 is greater than 15
8 is greater than 15
10 is less than 15
11 is less than 15
23 is greater than 15
52 is greater than 15
【问题讨论】:
-
我建议将
>替换为-gt进行算术测试。 -
这能回答你的问题吗? How can I compare numbers in Bash? - 领先的
0在这里不是问题,因为您的值不会以八进制更改;这完全是关于字典式字符串排序中“15”与“2”的位置。 -
@dawg 是的,这帮了很多忙,我从来不知道有这么大的区别。谢谢!
标签: bash