我想比较3套然后我写一个bash脚本但结果是错误的。 请指导我
#!/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
脚本
的结果root @ proxy:/ tmp#vi multiple_var_test&amp;&amp; ./multiple_var_test
正确是A - 答案是= A
正确是B - 答案是= A
正确是C - 答案是= A
正确是D - 答案是= A
答案 0 :(得分:3)
使用((
代替[[
来评估算术表达式。
您还需要在条件中使用&&
(和),而不是||
(或)。
试试这个:
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"
}
(注意:在算术表达式中,没有必要使用$
为变量添加前缀。)
答案 1 :(得分:0)
你的逻辑错误 - 应该是AND而不是OR,你需要用下面的比较运算符代替:
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"
}