我无法进行数值比较:

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;

问题是它比较了从第一位开始的数字,即9大于10,但1大于09。

如何将数字转换为一种类型以进行真正的比较?


当前回答

如果你有浮点数,你可以写一个函数然后使用它。例如

#!/bin/bash

function float_gt() {
    perl -e "{if($1>$2){print 1} else {print 0}}"
}

x=3.14
y=5.20
if [ $(float_gt $x $y) == 1 ] ; then
    echo "do stuff with x"
else
    echo "do stuff with y"
fi

其他回答

如果你有浮点数,你可以写一个函数然后使用它。例如

#!/bin/bash

function float_gt() {
    perl -e "{if($1>$2){print 1} else {print 0}}"
}

x=3.14
y=5.20
if [ $(float_gt $x $y) == 1 ] ; then
    echo "do stuff with x"
else
    echo "do stuff with y"
fi

还有一件好事,有些人可能不知道:

echo $(( a < b ? a : b ))

此代码将打印a和b中的最小数字

除了以上所有答案之外:

如果在单个If语句中有多个表达式,可以执行以下操作:

if (( $a % 2 == 0 )) && (( $b % 2 != 0));
  then
  echo "What you want to do"
fi

希望这有帮助!

此代码还可以比较浮点数。它使用AWK(它不是纯Bash)。然而,这不应该是一个问题,因为AWK是一个标准的POSIX命令,很可能默认情况下随操作系统一起提供。

$ awk 'BEGIN {return_code=(-1.2345 == -1.2345) ? 0 : 1; exit} END {exit return_code}'
$ echo $?
0
$ awk 'BEGIN {return_code=(-1.2345 >= -1.2345) ? 0 : 1; exit} END {exit return_code}'
$ echo $?
0
$ awk 'BEGIN {return_code=(-1.2345 < -1.2345) ? 0 : 1; exit} END {exit return_code}'
$ echo $?
1
$ awk 'BEGIN {return_code=(-1.2345 < 2) ? 0 : 1; exit} END {exit return_code}'
$ echo $?
0
$ awk 'BEGIN {return_code=(-1.2345 > 2) ? 0 : 1; exit} END {exit return_code}'
$ echo $?

要缩短使用时间,请使用以下功能:

compare_nums()
{
   # Function to compare two numbers (float or integers) by using AWK.
   # The function will not print anything, but it will return 0 (if the comparison is true) or 1
   # (if the comparison is false) exit codes, so it can be used directly in shell one liners.
   #############
   ### Usage ###
   ### Note that you have to enclose the comparison operator in quotes.
   #############
   # compare_nums 1 ">" 2 # returns false
   # compare_nums 1.23 "<=" 2 # returns true
   # compare_nums -1.238 "<=" -2 # returns false
   #############################################
   num1=$1
   op=$2
   num2=$3
   E_BADARGS=65

   # Make sure that the provided numbers are actually numbers.
   if ! [[ $num1 =~ ^-?[0-9]+([.][0-9]+)?$ ]]; then >&2 echo "$num1 is not a number"; return $E_BADARGS; fi
   if ! [[ $num2 =~ ^-?[0-9]+([.][0-9]+)?$ ]]; then >&2 echo "$num2 is not a number"; return $E_BADARGS; fi

   # If you want to print the exit code as well (instead of only returning it), uncomment
   # the awk line below and comment the uncommented one which is two lines below.
   #awk 'BEGIN {print return_code=('$num1' '$op' '$num2') ? 0 : 1; exit} END {exit return_code}'
   awk 'BEGIN {return_code=('$num1' '$op' '$num2') ? 0 : 1; exit} END {exit return_code}'
   return_code=$?
   return $return_code
}

$ compare_nums -1.2345 ">=" -1.2345 && echo true || echo false
true
$ compare_nums -1.2345 ">=" 23 && echo true || echo false
false

单线解决方案。

a=2
b=1
[[ ${a} -gt ${b} ]] && echo "true" || echo "false"

gt参考:https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html

&&参考:https://www.gnu.org/software/bash/manual/html_node/Shell-Arithmetic.html

[[…]]构造引用:https://www.gnu.org/software/bash/manual/bash.html#index-_005b_005b

${}引用:https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02 (2.6.2)

参数展开格式如下:${表达式}其中表达式由匹配的“}”之前的所有字符组成。由或在带引号的字符串中转义的任何“}”,以及嵌入式算术扩展、命令替换、,在确定匹配“}”。参数展开的最简单形式是:${参数}