【发布时间】:2017-09-03 07:26:30
【问题描述】:
我编写了这个脚本来比较 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
【问题讨论】:
-
要比较数字,请使用
-gt运算符来表示>,使用-lt表示<和-eq表示= -
@anubhava 但它对输入“2 和 3”给出了错误的答案
-
Comparing numbers in Bash 的可能重复项。