在Bash中比较数字

本文翻译自: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值