2017-06-06 113 views
1

有沒有辦法編寫一個更優雅的代碼,它考慮多種可能的組合而不是編寫冗長的if-else語句?在bash中改進long if-else語句

我有隨機生成的數據集像下面的那些,但有些行有時可能爲空。

{
semester1 grade1 final_grade1
semester1a grade1a final_grade1a
}

{
semester2 grade2 final_grade2
semester2a grade2a final_grade2a
}

我已經WR我的數據的所有可能的邏輯組合伊頓代碼:

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 

有沒有檢查這更好的辦法?

+1

什麼定義更好?任何代碼行數較少的代碼可能更難以讓另一個人維​​護。至少在這裏,意圖是什麼是無法理解的。 (將被bash精英的成員證明是錯誤的;-)。祝你好運。 – shellter

+0

[codereview.se]通常更歡迎在沒有提出特定技術問題的情況下改進工作代碼的問題。 –

+0

我喜歡這個問題。真值表(這本質上是這樣)是可愛的,但很少有腳本語言似乎支持它們作爲數據結構。對我來說,這感覺就像是在計算機科學中錯過了一次機會。 – sampablokuper

回答

1

此循環可能會這樣做。你需要的第二個爲第二學期或也許把它變成一個函數,然後使瓦爾地方:

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 
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是非常缺乏

+0

不幸的是,這些數據也將用於一些計算 – Afungus