方式一:先将小数的整数部分截取出来,再比较整数部分。
1 [root@ecs-s6-medium-2-linux-20200214130415 ~]# a=1.2 2 [root@ecs-s6-medium-2-linux-20200214130415 ~]# b=2.7 3 [root@ecs-s6-medium-2-linux-20200214130415 ~]# a_int=`echo ${a%.*}` 4 [root@ecs-s6-medium-2-linux-20200214130415 ~]# b_int=`echo ${b%.*}` 5 [root@ecs-s6-medium-2-linux-20200214130415 ~]# (($a_int < $b_int)) && echo "yes" 6 yes 7 [root@ecs-s6-medium-2-linux-20200214130415 ~]#
方式二:使用expr命令直接进行比较,但是这个比较方式好像有点bug,有部分数据比较时会失败。
1 [root@ecs-s6-medium-2-linux-20200214130415 ~]# a=789.97 2 [root@ecs-s6-medium-2-linux-20200214130415 ~]# b=6.98 3 [root@ecs-s6-medium-2-linux-20200214130415 ~]# ((`expr $a \> $b`)) && echo "yes" 4 yes 5 [root@ecs-s6-medium-2-linux-20200214130415 ~]# a=300.20 6 [root@ecs-s6-medium-2-linux-20200214130415 ~]# b=6.90 7 [root@ecs-s6-medium-2-linux-20200214130415 ~]# ((`expr $a \> $b`)) && echo "yes" # 比较这两个数的大小时,没有输出后面的结果 8 [root@ecs-s6-medium-2-linux-20200214130415 ~]#
方式三:使用bc命令转换。
1 [root@ecs-s6-medium-2-linux-20200214130415 ~]# a=789.97 2 [root@ecs-s6-medium-2-linux-20200214130415 ~]# b=6.98 3 [root@ecs-s6-medium-2-linux-20200214130415 ~]# [ `echo "$a>$b" |bc` -eq 1 ] && echo "yes" 4 yes 5 [root@ecs-s6-medium-2-linux-20200214130415 ~]# a=300.20 6 [root@ecs-s6-medium-2-linux-20200214130415 ~]# b=6.90 7 [root@ecs-s6-medium-2-linux-20200214130415 ~]# [ `echo "$a>$b" |bc` -eq 1 ] && echo "yes" 8 yes 9 [root@ecs-s6-medium-2-linux-20200214130415 ~]#