1

I want to compare 3 set then I write a bash script but the result is wrong. please guide me

#!/bin/bash
function SCORETYPE {
if [[ "$ENG1" < "$ENG2" || "$ENG3" < "$ENG4" || "$ENG5" > "$ENG6" ]]; then
    STYPE="A"
elif [[ "$ENG1" < "$ENG2" || "$ENG3" > "$ENG4" || "$ENG5" > "$ENG6" ]]; then
    STYPE="B"
elif [[ "$ENG1" > "$ENG2" || "$ENG3" < "$ENG4" || "$ENG5" < "$ENG6" ]]; then
    STYPE="C"
else
    STYPE="D"
fi

echo "$STYPE"
}

# DATA for  A
ENG1=10; ENG2=15; ENG3=5; ENG4=7; ENG5=45; ENG6=15
echo -n "Correct is A -- Answer is = "; SCORETYPE

# DATA for  B
ENG1=3; ENG2=10; ENG3=8; ENG4=7; ENG5=40; ENG6=38
echo -n "Correct is B -- Answer is = "; SCORETYPE

# DATA for  C
ENG1=12; ENG2=9; ENG3=6; ENG4=9; ENG5=32; ENG6=50
echo -n "Correct is C -- Answer is = "; SCORETYPE

# DATA for  D
ENG1=12; ENG2=9; ENG3=9; ENG4=8; ENG5=32; ENG6=50
echo -n "Correct is D -- Answer is = "; SCORETYPE

result of script

root@proxy:/tmp# vi multiple_var_test && ./multiple_var_test

Correct is A -- Answer is = A

Correct is B -- Answer is = A

Correct is C -- Answer is = A

Correct is D -- Answer is = A

1
  • 2
    you probably meant to use && instead of || ... the if else conditions don't make sense with the or || operator Commented Jan 20, 2014 at 17:53

2 Answers 2

3

Use (( instead of [[ in order to evaluate arithmetic expressions.

You also need to use && (and), not || (or) in your conditions.

Try this:

function SCORETYPE {
if (( ENG1 < ENG2 && ENG3 < ENG4 && ENG5 > ENG6 )); then
    STYPE="A"
elif (( ENG1 < ENG2 && ENG3 > ENG4 && ENG5 > ENG6 )); then
    STYPE="B"
elif (( ENG1 > ENG2 && ENG3 < ENG4 && ENG5 < ENG6 )); then
    STYPE="C"
else
    STYPE="D"
fi

echo "$STYPE"
}

(Note: Within an arithmetic expression, it is not necessary to prefix variables with a $.)

Sign up to request clarification or add additional context in comments.

Comments

0

You have your logic wrong - should be AND not OR, and you need to substitute your comparison operators with the below:

function SCORETYPE {
if [[ "$ENG1" -lt "$ENG2" && "$ENG3" -lt "$ENG4" && "$ENG5" -gt "$ENG6" ]]; then
    STYPE="A"
elif [[ "$ENG1" -lt "$ENG2" && "$ENG3" -gt "$ENG4" && "$ENG5" -gt "$ENG6" ]]; then
    STYPE="B"
elif [[ "$ENG1" -gt "$ENG2" && "$ENG3" -lt "$ENG4" && "$ENG5" -lt "$ENG6" ]]; then
    STYPE="C"
else
    STYPE="D"
fi

echo "$STYPE"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.