在bash中改进if-else语句

时间:2017-06-06 18:36:56

标签: bash shell unix if-statement scripting

有没有办法编写一个更优雅的代码,考虑多种可能的组合,而不是编写冗长的if-else语句?

我有随机生成的数据集,如下所示,但有些行有时可能为空。

  

{
   第一学期1最终成绩1    semester1a grade1a final_grade1a
  }

     

{
   第二学期二年级_最终成绩2    semester2a grade2a final_grade2a
  }

我已经为我的数据的所有可能逻辑组合编写了代码:

if [[ -n $semester1 && -z $semester1a && -z $semester2 && -z $semester2a ]]; then
echo "Section 1:$semester1=$grade1, final grade for $semester1=$final_grade1 ."

elif [[ -n $semester1 && -n $semester1a && -z $semester2 && -z $semester2a ]]; then
echo "Section 1:$semester1=$grade1, $semester1a=$grade1a, final grade for $semester1=$final_grade1, final grade for $semester1a=$final_grade1a."

elif [[ -n $semester1 && -n $semester1a && -n $semester2 && -z $semester2a ]]; then
echo "Section 1:$semester1=$grade1, $semester1a=$grade1a, final grade for $semester1=$final_grade1, final grade for $semester1a=$final_grade1a."
echo "Section 2:$semester2=$grade2, final grade for $semester2=$final_grade2 ."

elif [[ -n $semester2 && -z $semester2a && -z $semester1 && -z $semester1a ]]; then
echo "Section 2:$semester2=$grade2, final grade for $semester2=$final_grade2 ."

elif [[ -n $semester2 && -n $semester2a && -z $semester1 && -z $semester1a ]]; then
echo "Section 2:$semester2=$grade2, $semester2a=$grade2a, final grade for $semester2=$final_grade2, final grade for $semester2a=$final_grade2a, ."

elif [[ -n $semester2 && -n $semester2a && -n $semester1 && -z $semester1a ]]; then
echo "Section 1:$semester1=$grade1, final grade for $semester1=$final_grade1 ."
echo "Section 2:$semester2=$grade2, $semester2a=$grade2a, final grade for $semester2=$final_grade2, final grade for $semester2a=$final_grade2a, ."

elif [[ -n $semester2 && -n $semester2a && -n $semester1 && -n $semester1a ]]; then
echo "Section 1:$semester1=$grade1, $semester1a=$grade1a, final grade for $semester1=$final_grade1, final grade for $semester1a=$final_grade1a, ."
echo "Section 2:$semester2=$grade2, $semester2a=$grade2a, final grade for $semester2=$final_grade2, final grade for $semester2a=$final_grade2a, ."

fi

有更好的方法可以检查吗?

2 个答案:

答案 0 :(得分:1)

这个循环可能会这样做。你需要第二个学期的第二个学期,或者可能把它变成一个函数,然后让vars本地化:

if [[ -n $semester1 ]]; then
  section="Section 1:$semester1=$grade1"
  final="final grade for $semester1=$final_grade1"
  if [[ -n $semester1a ]]; then
    section="$section, $semester1a=$grade1a"
    final="$final, final grade for $semester1a=$final_grade1a."
  fi
  echo "$section, $final"
fi

答案 1 :(得分:0)

如果你没有做计算;我只想给未定义的变量分配一个默认值(“n / a”或许?)

: ${semester1:=n/a} ${semester1a:=n/a} ${semester2:=n/a} ${semester2a:=n/a}
: ${grade1:=n/a} ${grade1a:=n/a} ${grade2:=n/a} ${grade2a:=n/a}
: ${final_grade1:=n/a} ${final_grade1a:=n/a} ${final_grade2:=n/a} ${final_grade2a:=n/a}
echo "Section 1:$semester1=$grade1, $semester1a=$grade1a, final grade for $semester1=$final_grade1, final grade for $semester1a=$final_grade1a, ."
echo "Section 2:$semester2=$grade2, $semester2a=$grade2a, final grade for $semester2=$final_grade2, final grade for $semester2a=$final_grade2a, ."

但是,对于bash非常缺乏的列表和字典/散列/地图等数据结构,此任务会大声呼喊。