shell编程入门之成绩统计

备注:如果你是厂里的童鞋,写作业找到了这篇文章,希望你不要直接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]}; 

 

posted @ 2018-01-01 19:19  章鱼小年糕  阅读(669)  评论(0编辑  收藏  举报