shell成绩管理系统

#!/bin/bash
studentFile=./student.txt
majoyFile=./majoy.txt
scoreFile=./score.txt
TMP=./tmp  

#选项 
menu () {  
    echo "***************************************************************************"  
    echo -e "\t\t1.show student information"  
    echo -e "\t\t2.show majoy information"  
	echo -e "\t\t3.show score information" 
	echo -e "\t\t4.update student information"
	echo -e "\t\t5.insert into student"  
	echo -e "\t\t6.delete student information"	
	echo -e "\t\t7.sort student score" 
	echo -e "\t\t0.exit" 
    echo "***************************************************************************"  
    echo -n "Select:"  
}  

#增加学生信息
insertStudent () {  
	echo -e "insert into student" 
	#add id  
    while [ 1 ];do  
        echo -n "Student id(123000-123099):"  
        read id  
        flag=`echo $id|grep -c "^[0-9]\{6\}$"`  
        if [ $flag -eq 0 ] ; then  
            echo "id must 123000-123099"  
            continue   
        fi  
        flag2=`cat student.txt | awk -F "," '{print $1}' | grep -c ^${id}$`  
        if [ $flag2 -ge 1 ] ;then  
            echo "The id must be only one!"  
            continue  
        fi    
        break  
    done  
	#add name    
    while [ 1 ];do  
        echo -n "Student name(only a-z,A-Z and within 10 characters):"  
        read name  
        flag=`echo $name|grep -c "^[a-zA-Z]\{1,10\}$"`  
        if [ $flag -eq 0 ] ; then  
            echo "name must within 10 characters only a-z,A-Z"  
        fi  
		break
    done  
	#add majoyid    
	while [ 1 ]; do 	
		echo -n "Student majoyid(1000-1999):"  
        read majoyid  
        flag=`echo $majoyid|grep -c "^[0-9]\{4\}$"`  
        if [ $flag -eq 0 ] ; then  
            echo "ID must 1000-1999"  
            continue   
        fi  
        break  
	done
	#add type  
    while [ 1 ]; do  
        echo -e "Student type:"  
		echo -e "1.reading"  
		echo -e "2.suspended"   
		echo -e "3.dropout"
        read number
		case $number in
		1) type=reading ;;
		2) type=suspended ;;
		3) type=dropout ;;
		esac
		break
    done  
  
    echo -e "$id,$name,$majoyid,$type" >> $studentFile  
	echo "insert successful" 
}  

#增加专业信息
insertMajoy () {  
	echo -n "show information student:"  
} 

#增加成绩信息
insertScore () {
	echo -n "show information student:"  
} 

#显示学生信息
showStudent () {  
    echo -e "show information student:"  
	echo -e "id\tname\tmajoyid\ttype"  
	while read message
	do 
		arr=(${message//,/ })
		printf "%s\t%s\t%s\t%s" ${arr[0]} ${arr[1]} ${arr[2]} ${arr[3]}
		echo
	done < $studentFile
	echo 
}  

#显示专业信息
showMajoy () {  
    echo -e "show information majoy:"  
	echo -e "majoyid\tmajoyname"  
	while read message
	do 
		arr=(${message//,/ })
		printf "%s\t%s" ${arr[0]} ${arr[1]}
		echo
	done < $majoyFile
	echo 
}

#显示成绩信息
showScore () {  
	echo -e "show information score:"  
	echo -e "id\tname\tcource\tstype\tscore"  
	while read message
	do 
		arr=(${message//,/ })
		printf "%s\t%s\t%s\t%s\t%s" ${arr[0]} ${arr[1]} ${arr[2]} ${arr[3]} ${arr[4]}
		echo
	done < $scoreFile
	echo 
}  

#修改指定学号信息
updateStudent () {
	echo -e "update student information" 
    while [ 1 ];do  
        echo -n "input Student id(123000-123099):"  
        read id  
        flag=`echo $id|grep -c "^[0-9]\{6\}$"`  
        if [ $flag -eq 0 ] ; then  
            echo "id must 123000-123099"  
            continue   
        fi  
        flag2=`cat student.txt | awk -F "," '{print $1}' | grep -c ^${id}$`  
        if [ $flag2 -eq 0 ] ;then  
            echo "can't find id=$id student!"  
            continue  
        fi    
        break  
    done  
	#add name    
    while [ 1 ];do  
        echo -n "Student name(only a-z,A-Z and within 10 characters):"  
        read name  
        flag=`echo $name|grep -c "^[a-zA-Z]\{1,10\}$"`  
        if [ $flag -eq 0 ] ; then  
            echo "name must within 10 characters only a-z,A-Z"  
        fi  
		break
    done  
	#add majoyid    
	while [ 1 ]; do 	
		echo -n "Student majoyid(1000-1999):"  
        read majoyid  
        flag=`echo $majoyid|grep -c "^[0-9]\{4\}$"`  
        if [ $flag -eq 0 ] ; then  
            echo "ID must 1000-1999"  
            continue   
        fi  
        break  
	done
	#add type  
    while [ 1 ]; do  
        echo -e "Student type:"  
		echo -e "1.reading"  
		echo -e "2.suspended"   
		echo -e "3.dropout"
        read number
		case $number in
		1) type=reading ;;
		2) type=suspended ;;
		3) type=dropout ;;
		esac
		break
    done  
	num=`awk '{print $1}' $studentFile | grep -n "${id}" |sed 's/:.*//g'`    
	sed "${num}d" $studentFile > $TMP
	echo -e "$id,$name,$majoyid,$type" >> $TMP
	cat $TMP > $studentFile  
	echo > $TMP
	echo "Update successful" 
}

#删除指定学号的学生信息 
deleteStudent () {  
	while [ 1 ]; do
		echo -n "Input the student id:"  
		read id  
		flag=`grep -n "$id" $studentFile`  
		if [ "$flag" == "" -o "$id" == "" ]; then  
			echo "can't find id=$id student"  
			continue   
		fi  
      
		#get delete number  
		num=`awk '{print $1}' $studentFile | grep -n "${id}" |sed 's/:.*//g'`     
		echo -n "Are you sure delete id(Y or N):"  
		read sure  
		if [ "$sure" == "Y" -o "$sure" == "y" ] ; then  
			sed "${num}d" $studentFile > $TMP  
			cat $TMP > $studentFile  
			echo > $TMP
			echo "Deleted successful"  
		fi  
		break
	done
}

#按照数学成绩排名前10
sortStudentScoreMath () {
	echo -e "sort in math:" 
	i=0
	while read message
	do 
		arr=(${message//,/ })
		if [ "${arr[2]}" == "math" ] ; then
			sortId[i]=${arr[0]}
			sortName[i]=${arr[1]}
			sortMajoy[i]=${arr[2]}
			sortType[i]=${arr[3]}
			sortScore[i]=${arr[4]}
			i=$(($i+1))
		fi
	done < $scoreFile
	
	#冒泡排序
	n=${#sortScore[@]}
	for i in `seq 0 $n`
	do
		for (( j=n; j > i; j-=1 ))
		do
			if [[ ${sortScore[j-1]} < ${sortScore[j]} ]] 
			then
				temp1=${sortId[j]}
				sortId[j]=${sortId[j-1]}
				sortId[j-1]=$temp1
				
				temp2=${sortName[j]}
				sortName[j]=${sortName[j-1]}
				sortName[j-1]=$temp2
				
				temp3=${sortMajoy[j]}
				sortMajoy[j]=${sortMajoy[j-1]}
				sortMajoy[j-1]=$temp3
				
				temp4=${sortType[j]}
				sortType[j]=${sortType[j-1]}
				sortType[j-1]=$temp4
				
				temp5=${sortScore[j]}
				sortScore[j]=${sortScore[j-1]}
				sortScore[j-1]=$temp5
			fi
		done
	done
	
	echo -e "id\tname\tcource\tstype\tscore"  
	n=4
	for i in `seq 0 $n`
	do
		printf "%s\t%s\t%s\t%s\t%s" ${sortId[i]} ${sortName[i]} ${sortMajoy[i]} ${sortType[i]} ${sortScore[i]}
		echo
	done
}

#按照英语成绩排名前10
sortStudentScoreEnlish () {
	echo -e "sort in english:" 
	i=0
	while read message
	do 
		arr=(${message//,/ })
		if [ "${arr[2]}" == "english" ] ; then
			sortId[i]=${arr[0]}
			sortName[i]=${arr[1]}
			sortMajoy[i]=${arr[2]}
			sortType[i]=${arr[3]}
			sortScore[i]=${arr[4]}
			i=$(($i+1))
		fi
	done < $scoreFile
	
	#冒泡排序
	n=${#sortScore[@]}
	for i in `seq 0 $n`
	do
		for (( j=n; j > i; j-=1 ))
		do
			if [[ ${sortScore[j-1]} < ${sortScore[j]} ]] 
			then
				temp1=${sortId[j]}
				sortId[j]=${sortId[j-1]}
				sortId[j-1]=$temp1
				
				temp2=${sortName[j]}
				sortName[j]=${sortName[j-1]}
				sortName[j-1]=$temp2
				
				temp3=${sortMajoy[j]}
				sortMajoy[j]=${sortMajoy[j-1]}
				sortMajoy[j-1]=$temp3
				
				temp4=${sortType[j]}
				sortType[j]=${sortType[j-1]}
				sortType[j-1]=$temp4
				
				temp5=${sortScore[j]}
				sortScore[j]=${sortScore[j-1]}
				sortScore[j-1]=$temp5
			fi
		done
	done
	
	echo -e "id\tname\tcource\tstype\tscore"  
	n=4
	for i in `seq 0 $n`
	do
		printf "%s\t%s\t%s\t%s\t%s" ${sortId[i]} ${sortName[i]} ${sortMajoy[i]} ${sortType[i]} ${sortScore[i]}
		echo
	done
}

#按照总成绩排名前10
sortStudentScoreallSum () {
	echo -e "sort in allSum:"
	i=0;
	while read message
	do 
		studentArr=(${message//,/ })
		sortId[i]=${studentArr[0]}
		sortName[i]=${studentArr[1]}
		
		while read line
		do
			scoreArr=(${line//,/ })
			if [[ ${studentArr[0]} = ${scoreArr[0]} ]] ; then
				allSum[i]=$((${allSum[i]}+${scoreArr[4]}))
			fi
		done < $scoreFile
		i=$(($i+1))
	done < $studentFile
	
	#冒泡排序

	n=${#allSum[@]}
				
	for ii in `seq 0 $n`
	do
		for (( j=n; j > ii; j-=1 ))
		do
			if [[ "${allSum[j-1]}" -lt "${allSum[j]}" ]] 
			then
				temp1=${sortId[j]}
				sortId[j]=${sortId[j-1]}
				sortId[j-1]=$temp1
				
				temp2=${sortName[j]}
				sortName[j]=${sortName[j-1]}
				sortName[j-1]=$temp2
				
				temp3=${allSum[j]}
				allSum[j]=${allSum[j-1]}
				allSum[j-1]=$temp3
			fi
		done
	done

	n=4
	echo -e "id\tname\tallScore"  
	for i in `seq 0 $n`
	do
		printf "%s\t%s\t%s" ${sortId[i]} ${sortName[i]} ${allSum[i]}
		echo
	done
	
}

#排序学生成绩
sortStudentScore () {
	echo -e "sort top 5:"  
	echo -e "1.math"  
	echo -e "2.enlish"   
	echo -e "3.allSum"
    read number
	case $number in
	1) sortStudentScoreMath ;;
	2) sortStudentScoreEnlish ;;
	3) sortStudentScoreallSum ;;
	esac
}

#退出
quit () {  
    echo "Thank you Bye!"  
    exit  
}  

#main  主程序
main () {
while [ 1 ] ; do
	menu
	read number
	case $number in
	1) showStudent ;;
	2) showMajoy ;;
	3) showScore ;;
	4) updateStudent ;;
	5) insertStudent ;;
	6) deleteStudent ;;
	7) sortStudentScore ;;
	0) quit ;;
	esac
done
}
main
下载地址: http://download.csdn.net/detail/hong0220/6646291
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值