shell編程入門之成績統計
阿新 • • 發佈:2018-01-01
童鞋 elif and bin 裏的 ech 文章 找到 -a
備註:如果你是廠裏的童鞋,寫作業找到了這篇文章,希望你不要直接copy^_^,其實試著自己敲一遍會對你幫助很大。
題目要求是把student.txt內的成績進行各個階段的統計,文本格式都是"學號:成績"。
下面是我的做法:
#! bin/bash s=$(cat score.txt | cut -d : -f 2 | tr " " ","); arr=($s); num=(0,0,0,0,0); grade=(0,0,0,0,0); for x in ${arr[@]};do if [ $((x)) -lt 100 -a $((x)) -gt 90 ];then num[0]=$(($((num[0])) + 1)); grade[0]=$((grade[0] + $((x)))); elif [ $((x)) -gt 80 ];then num[1]=$(($((num[1])) + 1)); grade[1]=$((grade[1] + $((x)))); elif [ $((x)) -gt 70 ];then num[2]=$(($((num[2])) + 1)); grade[2]=$((grade[2] + $((x)))); elif [ $((x)) -gt 60 ];then num[3]=$(($((num[3])) + 1)); grade[3]=$((grade[3] + $((x)))); else num[4]=$(($((num[4])) + 1)); grade[4]=$((grade[4] + $((x)))); fi done echo the avg score of 90~100 is $((${grade[0]} / ${num[0]})),and the number of student is ${num[0]}; echo the avg score of 80~89 is $((${grade[1]} / ${num[1]})),and the number of student is ${num[1]}; echo the avg score of 70~79 is $((${grade[2]} / ${num[2]})),and the number of student is ${num[2]}; echo the avg score of 60~69 is $((${grade[3]} / ${num[3]})),and the number of student is ${num[3]}; echo the avg score of ~60 is $((${grade[4]} / ${num[4]})),and the number of student is ${num[4]};
shell編程入門之成績統計