user6389648 user6389648 - 3 months ago 9
Linux Question

Expected Output not coming while comparing 2 numbers in linux


https://www.hackerrank.com/challenges/bash-tutorials---comparing-numbers

HackerRank Problem:Comparing Numbers


I am writing a simple problem in linux that says Given two integers, X and Y, identify whether X is greater than Y or X is less than Y or X is equal to Y:


Below is my Code:

read x y

if ((x>y)); then
echo "x is greater than y";
elif ((x==y)); then
echo "x is equal to y";
else
echo "x is less than y";
fi;


But I am not getting correct answer in some of the Shells:
I don't know how to correct and why isn't not working in some of the bash shells.


Can anyone guide me Why and How to correct this?
Thanx in advance.

Answer

Two things: If x and y are variables, why a and b ? Also even if you declare read a b, you need to add $a,$b inside if. I don't think it would work in any shell.

read x
read y

if (($x>$y)); then  
echo "x is greater than y"; 
elif (($x==$y)); then  
echo "x is equal to y"; 
else  
echo "x is less than y"; 
fi;