I wrote this script to compare 2 numbers in bash but it gives me wrong answers for some numbers. like if I give it 2&2 for input , it gives me "X is greater than 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
You can try with bash arithmetic contexts:
#!/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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With