shell成绩管理系统

原创 2013年12月02日 21:32:05
#!/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
版权声明:本文为博主原创文章,未经博主允许不得转载。

简单的shell 成绩管理系统

最近在看张昊写的linux shell 编程从入门到精通,喜欢这本书的风格,简单易懂,还带一点幽默,读起来有味。    刚好看完第8章的awk,老师要求完成个程序,如下                ...

Shell程序---学生管理系统

读过大学,学过Linux的人懂得这种蛋疼的感觉.....Shell程序,蛮贴出来吧! 实验内容 Ø 作业:学生成绩管理程序 Ø 功能: 对学生成绩进行管理。要求实现数据的基本操作:学院和学生信息...

学生成绩管理系统(Linux Shell版)

Github完整代码链接 主要包括语言包(系统有中英文)和主系统程序以及辅助登录注册模块 主系统(main.sh) #!/bin/bash. lang.shprint() { if [ $1...

shell脚本实现学生管理系统

刚刚接触shell脚本,为巩固一下知识,随手写了一个学生管理系统 简单功能实现,欢迎交流...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

shell 编程实验--实现学生成绩管理系统

shell 编程实验--实现学生成绩管理系统

学生成绩管理系统 - Linux Shell 编程

#! /bin/bashDIALOG=/usr/bin/dialogTMP="./tmp/tmp.$$"FILE_FACULTY="./file/Faculty.dat"FILE_STUDENT="....

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Linux--用shell写一个学生成绩管理系统

基本的操作 增加 删除 查询 展示全部学生信息 vim编辑保存信息的文件 遇到的问题1.增加学生信息时 增加学生信息时,我用的echo重定向到文件里,这里开始用的一个>,会导致覆盖。以前...

Shell学生成绩管理系统

#!/usr/bash #Output file save the students information FILE=./zhubinqiang_v1.0 #temp file TMP=./.tm...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:shell成绩管理系统
举报原因:
原因补充:

(最多只允许输入30个字)