0

I am trying to code a script that will tell the user if a triangle is isosceles, equilateral, or scalene. The error is occuring in line 7 (The elif line)

#!/bin/bash
read -p "Enter a number: " x
read -p "Enter a number: " y
read -p "Enter a number: " z
let "a = x + y + z"
if [ $x -eq $y ] && [ $y -eq $z ]
then echo "EQUILATERAL"
elif [[[ $x -eq $y ] && [ $y != $z ]] || [[ $x -eq $z ] && [ $z != $y ]] || [[ $y -eq $z ] && [ $z != $x ]]]
then echo "ISOSCELES"
elif [ $a -gt 1000 ]
then echo "Cannot equal more than 1000"
fi

I do realize that I could do the same thing with multiple elif lines, but I also have another elif as well and I want to keep it clean. Thanks all!

phd
  • 82,685
  • 13
  • 120
  • 165
Hirschy
  • 21
  • 5
  • When you start a test with `[[`, end it with `]]`; when you start a test with `[`, end it with `]`. – Jonathan Leffler Mar 10 '19 at 06:00
  • 1
    Possible duplicate of [Boolean operators ( &&, -a, ||, -o ) in Bash](https://stackoverflow.com/q/20449680/608639), [Simple logical operators in Bash](https://stackoverflow.com/q/6270440/608639), etc. – jww Mar 10 '19 at 06:53
  • FYI: I removed the "linux" tag. Please see its description, it should then become clear why. – Ulrich Eckhardt Mar 10 '19 at 10:00

1 Answers1

-1

It seems like you think square brackets in the shell are like parentheses in C-style programming languages. That's not how they work. [ is a synonym for the test command, the condition it introduces ends with ]. And [[ is a special token that introduces a conditional expression, which ends with ]]. You can't mix them up, you can't add additional brackets like [[[, and they don't nest.

The grouping operators in the shell are { ... } and ( ... ); the latter also creates a subshell.

elif ( [[ $x -eq $y ]] && [[ $y != $z ]] ) || ( [[ $x -eq $z ]] && [[ $z != $y ]] ) || ( [[ $y -eq $z ]] && [[ $z != $x ]] )
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • That uses a couple of sub-shells. – Jonathan Leffler Mar 10 '19 at 06:00
  • @JonathanLeffler I don't care. Using curly braces would require adding `;` before the `}`, which is ugly. – Barmar Mar 10 '19 at 06:09
  • @Hirschy Note that in this exact case, as you have already checked for equilateral triangles, you could just simplify the test: `elif [ $x -eq $y ] || [ $x -eq $z ] || [ $y -eq $z ]`. But I guess your Q is more about the shell syntax, which is indeed quite confusing some times... – yolenoyer Mar 10 '19 at 07:37
  • Unlike every other programming language, in bash `&&` does *not* have higher precedence than `||`. – John Kugelman Mar 10 '19 at 13:44
  • @JohnKugelman [this site](https://www.tldp.org/LDP/abs/html/opprecedence.html) claims it does, is it wrong? I can't find something in the Bash Manual. – Barmar Mar 10 '19 at 15:08
  • @Barmar The bash manual says: "Of these list operators, `&&` and `||` have equal precedence, followed by `;` and `&`, which have equal precedence." I only discovered this when it personally bit me. It took me a long time to track the bug down to this surprising lack of precedence. – John Kugelman Mar 10 '19 at 22:57