This is my program:
#!/bin/bash
read x y z
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:
command not found
CodePudding user response:
$x , $y , $z does not contain any valid bash command., basically ` is not required.
Instead of
if [ `$x -eq $y` ]
you need to use
if [ $x -eq $y ]
CodePudding user response:
Working example:
#!/bin/bash -x
read x y z
if [[ $x -eq $y && $y -eq $z && $z -eq $x ]]
then
echo "EQUILATERAL"
elif [[ $x -eq $y || $z -eq $y || $z -eq $x ]]
then
echo "ISOSCELES"
else
echo "SCALENE"
fi
Output:
~# ./1.sh
read x y z
4 4 4
[[ 4 -eq 4 ]]
[[ 4 -eq 4 ]]
[[ 4 -eq 4 ]]
echo EQUILATERAL
EQUILATERAL
~# ./1.sh
read x y z
3 3 4
[[ 3 -eq 3 ]]
[[ 3 -eq 4 ]]
[[ 3 -eq 3 ]]
echo ISOSCELES
ISOSCELES
~# ./1.sh
read x y z
3 4 5
[[ 3 -eq 4 ]]
[[ 3 -eq 4 ]]
[[ 5 -eq 4 ]]
[[ 5 -eq 3 ]]
echo SCALENE
SCALENE
CodePudding user response:
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