• Uncategorized

#### Question Detail

This is my program:

``````#!/bin/bash

if [ `\$x -eq \$y` -o `\$z -eq \$y` -o `\$z -eq \$x` ]
then
echo "ISOSCELES"
elif [ `"\$x" -eq "\$y"` -a `\$y -eq \$z` -a `\$z -eq \$x` ]

then
echo "EQUILATERAL"
else
echo "SCALENE"

fi
``````

and this is question:

Given three integers (X,Y, and Z) representing the three sides of a
triangle, identify whether the triangle is scalene, isosceles, or
equilateral.

If all three sides are equal, output EQUILATERAL. Otherwise, if any
two sides are equal, output ISOSCELES. Otherwise, output SCALENE.

I found this kind of error:

\$x , \$y , \$z does not contain any valid bash command., basically ` is not required.

``````if [ `\$x -eq \$y` ]
``````

you need to use

``````if [ "\$x" -eq "\$y" ]
``````

You should check your input for positive integers.
I wrote the test for Isosceles as a multiplication: funnier, not better.

``````#!/bin/bash
read -p "Give the length of the 3 sides (as integers): " x y z

if (( x == y )) && (( y == z)); then
echo "EQUILATERAL"
elif (( (x-y) * (y-z) * (z-x) == 0 )); then
echo "ISOSCELES"
else
echo "SCALENE"
fi
``````

Working example:

``````#!/bin/bash -x

if [ "\$x" -eq "\$y" ] && [ "\$y" -eq "\$z" ]
then
echo "EQUILATERAL"
elif [ "\$x" -eq "\$y" ] || [ "\$z" -eq "\$y" ] || [ "\$z" -eq "\$x" ]
then
echo "ISOSCELES"
else
echo "SCALENE"
fi
``````

Output:

``````~# ./1.sh
3 3 3
+ '[' 3 -eq 3 ']'
+ '[' 3 -eq 3 ']'
+ echo EQUILATERAL
EQUILATERAL

~# ./1.sh
3 3 4
+ '[' 3 -eq 3 ']'
+ '[' 3 -eq 4 ']'
+ '[' 3 -eq 3 ']'
+ echo ISOSCELES
ISOSCELES

~# ./1.sh