'Write a shell script to find the largest among three numbers. Get user inputs and display the result

Write a shell script to find the largest among three numbers. Get user inputs and display the result.

Sample input 1:

10
20
30

Sample output 1:

30 is largest number

Sample input 2:

10
10
10

Sample output 2:

All the three numbers are equal

Sample input 3:

10
10
1

Sample output 3:

I cannot figure out which number is largest

Bash code:

#!bin/bash
read a b c
if [ $a -eq $b -a $a -eq $c ]; then
    echo "All the three numbers are equal"
elif [[ $a -eq $b || $b -eq $c || $c -eq $a ]]; then
    echo "I cannot figure out which number is largest"
else
    if [ $a -gt $b -a $a -gt $c ]; then
        echo "$a is biggest number"
    elif [ $b -gt $a -a $b -gt $c ]; then
        echo "$b is biggest number"
    elif [ $c -gt $a -a $c -gt $b ]; then
        echo "$c is biggest number"
    fi
fi

Testcases to be passed: There are three test cases to be passed, but I am able to pass only one test case.

  1. Different values (passed). Passed test case
  2. Unique values (failed). Failed test case
  3. Equal values (failed). Failed test case

Actual result when evaluating the code:

Evaluation failed

Note: Even though I am getting actual result as expected result which is mentioned in question, I am able to pass the only one test case (different values) and remaining two test cases (equal values and unique values) are failing. I am even unable to spot the error.

Some errors I got when I tried in these ways:

Error while giving two equal values and one special character Error while two different values and one special character as input Error when giving less than 3 inputs Error while giving more than 3 inputs



Solution 1:[1]

read n1
read n2
read n3
if [[ $n1 == 0 || $n2 == 0 || $n3 == 0 ]]; then
    echo "command line arguments are missing"
elif [[ $n1 == $2 && $n2 == $n3 ]]; then
    echo "All the three numbers are equal"
elif [[ $n1 == $n2 || $n2 == $n3 || $n3 == $n1 ]]; then
    echo "I cannot figure out which number is biggest"
else
    if [[ $n1 > $n2 && $n1 > $n3 ]]; then 
        echo "$n1 is Biggest number"
    elif [[ $n2 > $n1 && $n2 > $n3 ]]; then
        echo "$n2 is Biggest number"
    else
        echo "$n3 is Biggest number"
    fi
fi

Solution 2:[2]

The error is not in your code. It is in the echo statement. You have to print the exact statement Accenture asked in the description. Instead of biggest number, it should be largest number.

I too was stuck on this frustrating issue and finally realized where things were going wrong.

Solution 3:[3]

There is a problem in the logic of the program. According to your code if the user enters ( 10, 10 , 1 ) it returns "I can't find out which number is greater" which is correct for that particular entry but if the user enters (20,10,10) your code still returns "I can't find out blah blah" Rather it should return "20 is largest number"

Solution 4:[4]

#!/bin/bash
read num1
read num2
read num3
if [ $num1 == $num2 -a $num1 == $num3 ]
then
 echo "All the three numbers are equal"
elif [ $num1 == $num2 -o $num1 == $num3 -o $num2 == $num3 ]
then
 echo "I cannot figure out which number is largest"
else
 if [ $num1 -gt $num2 -a $num1 -gt $num3 ]
 then
   echo "$num1 is largest numer"
 elif [ $num2 -gt $num1 -a $num2 -gt $num3 ]
 then
   echo "$num2 is largest number"
 else
   echo "$num3 is largest number"
 fi
fi

Solution 5:[5]

I had the same issues. It took a little troubleshooting.

read v1
read v2
read v3

if [[ $v1 == $v2 && $v1 == $v3 ]]; then
    echo "All the three numbers are equal"
elif [[ $v1 == $v2 || $v2 == $v3 || $v3 == $v1 ]]; then
    echo "I cannot figure out which number is largest"
else
    if [[ $v1 > $v2 && $v1 > $v3 ]]; then
        echo "$v1 is largest number"
    elif [[ $v2 > $v3 && $v2 > $v1 ]]; then
        echo "$v2 is largest number"
    elif [[ $v3 > $v1 && $v3 > $v2 ]]; then
        echo "$v3 is largest number"
    fi
fi

Solution 6:[6]

passed all test cases

` 
read n1
read n2
read n3

if (( $n1==$n2 & $n2!=$n3 ))
then
  echo "I cannot figure out which number is largest"
elif (( $n1==$n2  &  $n1==$n3 ))
then 
 echo "All the three numbers are equal"
 else
   if (( $n1>=$n2 & $n1>=$n3 ))
   then
    echo "$n1 is largest number"
   elif (( $n2>=$n1 & $n2>=$n3 ))
   then
    echo "$n2 is largest number"
   else
    echo "$n3 is largest number"
 fi
fi`

Solution 7:[7]

read n1
read n2
read n3
if [[ $n1 == $n2 && $n2 == $n3 ]]; then #Note : There should be space between $variable and == operator
    echo "All the three numbers are equal"
elif [[ $n1 == $n2 || $n2 == $n3 || $n1 == $n3 ]]; then
    echo "I cannot figure out which number is largest"
elif [[ $n1 > $n2 && $n1 > $n3 ]]; then
    echo "$n1 is largest number"
elif [[ $n2 > $n1 && $n2 > $n3 ]]; then
    echo "$n2 is largest number"
else
    echo "$n3 is largest number"
fi

Solution 8:[8]

Accept any three numbers ,compare them using if loop ,print output using echo Used if loop only, you can use elif loop also

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Roberto Caboni
Solution 2 Saswata Bhattacharya
Solution 3 Prabhod Urs
Solution 4 Rohit Jadhav
Solution 5 Dharman
Solution 6 tIrU
Solution 7
Solution 8