本文翻译自:Comparing numbers in Bash
I'm starting to learn about writing scripts for the bash terminal, but I can't work out how to get the comparisons to work properly. 我开始学习有关为bash终端编写脚本的知识,但是我无法弄清楚如何使比较正常工作。 The script I'm using is: 我使用的脚本是:
echo "enter two numbers";
read a b;
echo "a=$a";
echo "b=$b";
if [ $a \> $b ];
then
echo "a is greater than b";
else
echo "b is greater than a";
fi;
The problem is that it compares the number from the first digit on, ie 9 is bigger than 10, but 1 is greater than 09. 问题在于,它会比较从第一个数字开始的数字,即9大于10,但1大于09。
How can I convert the numbers into a type to do a true comparison? 如何将数字转换为类型以进行真正的比较?
#1楼
参考:https://stackoom.com/question/1GKXk/在Bash中比较数字
#2楼
In bash, you should do your check in arithmetic context : 在ba