skip to Main Content
a=10
b=20
c=30

if [[ $a -gt $b && $a -gt $c ]]
then
echo "A is the greater number"
if [[ $b -gt $a && $b -gt $c ]]
then
echo "B is the greater number"
else
echo "C is the greate number"
fi

I tried doing dos2unix formatting, adding another ‘fi’ but still code throwing same error


EDIT by Ed Morton: here’s your code with common indenting:

a=10
b=20
c=30

if [[ $a -gt $b && $a -gt $c ]]
then
    echo "A is the greater number"
    if [[ $b -gt $a && $b -gt $c ]]
    then
        echo "B is the greater number"
    else
        echo "C is the greate number"
    fi

I expect the problem is no longer a mystery if you format your code that way.

2

Answers


  1. You have two if statements but only one fi. Try using elif for your second case instead:

    a=10
    b=20
    c=30
    
    if [[ $a -gt $b && $a -gt $c ]]; then
        echo "A is the greater number"
    elif [[ $b -gt $a && $b -gt $c ]]; then
        echo "B is the greater number"
    else
        echo "C is the greater number"
    fi
    
    Login or Signup to reply.
  2. It a better option to split test logic operators outside of brackets.

    Here is an example of another implementation of your code:

    a=10
    b=20
    c=30
    
    if [ "$a" -gt "$b" ] && [ "$a" -gt "$c" ]; then
      gtst='A'
    elif [ "$b" -gt "$a" ] && [ "$b" -gt "$c" ]; then
      gtst='B'
    else
      gtst='C'
    fi
    printf '%s is the greatest number.n' "$gtst"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search