如何在 Bash 中比較數字? - How can I compare numbers in Bash?

問題:

I'm unable to get numeric comparisons working:我無法進行數字比較:

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?如何將數字轉換爲類型以進行真正的比較?


解決方案:

參考一: https://en.stackoom.com/question/1GKXk
參考二: https://stackoom.com/question/1GKXk
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章