比较 bash 脚本中的数字

Kei*_*pro 1 linux bash shell terminal

我写了这个脚本来比较 bash 中的 2 个数字,但它给了我一些数字的错误答案。就像如果我给它 2&2 输入,它会给我“X 大于 Y”

#!/bin/bash 
read num1
read num2
if [ $num1 > $num2 ]
    then 
        echo "X is greater than Y"
elif [ $num1 < $num2 ]
    then 
        echo "X is less than Y"
elif [ $num1 = $num2 ]
    then 
        echo "X is equal to Y"
fi 
Run Code Online (Sandbox Code Playgroud)

ifm*_*fma 5

您可以尝试使用 bash 算术上下文:

#!/bin/bash 
read num1
read num2
if (( num1 > num2 ))
    then 
        echo "X is greater than Y"
elif (( num1 < num2 ))
    then 
        echo "X is less than Y"
elif (( num1 == num2 ))
    then 
        echo "X is equal to Y"
fi 
Run Code Online (Sandbox Code Playgroud)