• Uncategorized

About linux : while-running-this-programe-i-occured-one-error-as-command-not-found

Question Detail

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

Question Answer

$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" ]

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

read x y z

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
+ read x y z
3 3 3
+ '[' 3 -eq 3 ']'
+ '[' 3 -eq 3 ']'
+ 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

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.