读过大学,学过Linux的人懂得这种蛋疼的感觉.....Shell程序,蛮贴出来吧!
实验内容
Ø 作业:学生成绩管理程序
Ø 功能:
对学生成绩进行管理。要求实现数据的基本操作:学院和学生信息以及学生成绩的增加,修改,删除,统计
Ø 构造三个类似数据库的文本文件:
第一个为学院信息文件,包含字段:
学院编号(唯一),学院名称
第二个为学生信息文件,包含字段:
学号(唯一),学生姓名,所在学院编号,说明(休学suspended,退学dropout)
第三个为学生成绩文件,包含字段:
学号(唯一),学生姓名,科目名称,成绩;说明(期考final,补考makeup)
说明:每个记录占一行;分隔符可以自己选定,建议用”,”; 编码规则自己定;文件名自己定
程序功能:要求实现4个功能,每个功能作为一个函数
1.向文件中插入记录
2.显示文件中的每条记录的每个字段值
3.从文件中修改指定学号的记录
4.对学生成绩进行统计(包括每个学生总成绩;每个学科前十名和总成绩前二十名统计)
截图:
#!/bin/bash #!/bin/awk -f DBStudentInfoPath="/home/xiaoyaomeng/LinuxHomeWork/DBStudentInfo.txt" DBCollegeInfoPath="/home/xiaoyaomeng/LinuxHomeWork/DBCollegeInfo.txt" DBGradeInfoPath="/home/xiaoyaomeng/LinuxHomeWork/DBGradeInfo.txt" insert_student_info(){ echo "请输入学生的编号(如果文件中已存在则插入失败!): " read studentNo export existFlag=false #判断是否存在编号和输入的一样的学生记录 #cat /home/xiaoyaomeng/LinuxHomeWork/DBStudentInfo.txt | while read line while read line do lineStuNo=${line/,*/} #echo "数字$lineStuNo,数字2$studentNo" if [ "$lineStuNo" = "$studentNo" ]; then { export existFlag=true break } fi done<$DBStudentInfoPath #echo "$existFlag" if [ "$existFlag" = "true" ]; then echo "输入的编号$studentNo已经存在了,插入失败!" else { export existFlag=false echo "请输入该学生的姓名:" read stuName echo "请输入该学生所在院校编号(必须是学院信息记录文件中有的学院编号):" read stuCollegeNo while read line do collegeNo=${line/,*/} if [ "$collegeNo" = "$stuCollegeNo" ]; then { export existFlag=true break } fi done<$DBCollegeInfoPath if [ "$existFlag" = "false" ]; then { echo "输入的学院编号$stuCollegeNo不存在于学院信息记录文件中($DBCollegeInfoPath)" } else { echo "请输入该学生的说明信息(1.inschool 2.suspended 3.dropout):" read sayInfo case $sayInfo in 1) echo "$studentNo,$stuName,$stuCollegeNo,inschool" >> $DBStudentInfoPath echo "插入学生记录成功($studentNo,$stuName,$stuCollegeNo,inschool)" ;; 2) echo "$studentNo,$stuName,$stuCollegeNo,suspended" >> $DBStudentInfoPath echo "插入学生记录成功($studentNo,$stuName,$stuCollegeNo,suspended) " ;; 3) echo "$studentNo,$stuName,$stuCollegeNo,dropout" >> $DBStudentInfoPath echo "插入学生记录成功($studentNo,$stuName,$stuCollegeNo,dropout) " ;; *) echo "说明信息填写有误,插入记录失败" ;; esac } fi } fi } delete_student_info(){ echo "请输入要删除的学生编号(若学生信息记录文件中不存在,则删除失败): " read studentNo export nowline=0; export delCount=0; while read line do export nowline=$(($nowline + 1)) echo $line":"$nowline lineStudentNo=${line/,*/} if [ "$lineStudentNo" = "$studentNo" ]; then { echo $nowline "是当前行" sed "$nowline"d $DBStudentInfoPath > $DBStudentInfoPath"_back" export delCount=$(($delCount + 1)) mv $DBStudentInfoPath"_back" $DBStudentInfoPath } fi done<$DBStudentInfoPath #mv $DBStudentInfoPath"_back" $DBStudentInfoPath echo "删除操作处理结束: 被删除的学生编号为$studentNo,删除的学生个数为$delCount" } insert_college_info(){ echo "请输入要加入的学院编号(若学院记录文件中存在,则插入失败):" read collegeNo export existFlag=false while read line do lineCollegeNo=${line/,*/} if [ "$lineCollegeNo" = "$collegeNo" ]; then { export existFlag=true break } fi done<$DBCollegeInfoPath if [ "$existFlag" == "true" ]; then { export existFlag=false echo "学院记录文件中已经存在此学院编号记录了,插入失败" } else { echo "请输入学院的名称: " read collegeName echo "$collegeNo,$collegeName" >> $DBCollegeInfoPath echo "学院记录插入成功,插入的记录为($collegeNo,$collegeName)" } fi } delete_college_info(){ echo "请输入要删除的学院编号(若学院记录文件中不存在则删除失败): " read collegeNo export nowline=0; export delCount=0; while read line do export nowline=$(($nowline + 1)) nowCollegeNo=${line/,*/} if [ "$nowCollegeNo" = "$collegeNo" ]; then { sed "$nowline"d $DBCollegeInfoPath > $DBCollegeInfoPath"_back" export delCount=$(($delCount + 1)) mv $DBCollegeInfoPath"_back" $DBCollegeInfoPath } fi done<$DBCollegeInfoPath #mv $DBCollegeInfoPath"_back" $DBCollegeInfoPath echo "删除学院记录完成:删除的学院编号为$collegeNo,删除的个数为$delCount" } insert_stugrade_info(){ echo "请输入该成绩对应的学生编号(若学生记录文件中不存在该编号则插入失败): " read studentNo export existFlag=false export nowline=0; export stuName="" while read line do lineStudentNo=${line/,*/} nowline=$(($nowline + 1)) if [ "$lineStudentNo" = "$studentNo" ]; then { export existFlag=true #如果是有这个学生编号的话,把该学生的姓名取出来 export stuName=`echo "$line"|awk -F',' '{print $2}'` #echo $stuName break } fi done<$DBStudentInfoPath if [ "$existFlag" = "false" ]; then { echo "sorry,学生记录文件中不存在学生编号为$studentNo,插入失败" } else { export existFlag=false echo "请输入该成绩对应的科目名称: " read objectName echo "请输入该成绩的分数(0<=grade<=100): " read gradeResult if [ $gradeResult -le 100 ] && [ $gradeResult -ge 0 ]; then { # echo $gradeResult echo "请输入该成绩的说明信息(1.期考final 2.补考makeup)" read sayInfo case $sayInfo in 1) echo "$studentNo,$stuName,$objectName,$gradeResult,final" >> $DBGradeInfoPath echo "插入成功($studentNo,$stuName,$objectName,$gradeResult,final)" ;; 2) echo "$studentNo,$stuName,$objectName,$gradeResult,makeup" >> $DBGradeInfoPath echo "插入成功($studentNo,$stuName,$objectName,$gradeResult,makeup)" ;; *) echo "输入有误(仅能选1 or 2),插入失败" ;; esac } else { echo "输入的成绩不符合要求(0<=grade<=100),插入失败" } fi } fi } delete_stugrade_info(){ echo "输入要删除成绩的学生的编号(存在于学生记录文件): " read studentNo export existFlag=false export delCount=0; export delLine=0; while read line do lineStudentNo=${line/,*/} if [ "$lineStudentNo" = "$studentNo" ]; then { export existFlag=true break } fi done<$DBStudentInfoPath if [ "$existFlag" = "true" ]; then { export existFlag=false #while read line # do # delLine=$(($delLine + 1)) # lineStudentNo=${line/,*/} # if [ "$lineStudentNo" = "$studentNo" ]; # then { # delCount=$(($delCount + 1)) # sed "$delLine"d $DBGradeInfoPath > $DBGradeInfoPath"_back" #mv $DBGradeInfoPath"_back" $DBGradeInfoPath # } # fi # done<$DBGradeInfoPath sed -i '/$studentNo/d' $DBGradeInfoPath > $DBGradeInfoPath"_back" mv $DBGradeInfoPath"_back" $DBGradeInfoPath echo "删除学生成绩成功:被删除学生编号$studentNo" } else { echo "学生记录文件中不存在该学生编号$studentNo,删除失败" } fi } update_student_info(){ echo "请输入要修改的学生编号(默认仅可修改学生自己的信息):" read studentNo stringLine=`grep $studentNo $DBStudentInfoPath` if [ "$stringLine" != "" ]; then { stuName=`echo "$stringLine"|awk -F',' '{print $2}'` stuCollegeNo=`echo "$stringLine"|awk -F',' '{print $3}'` studentSay=`echo "$stringLine"|awk -F',' '{print $4}'` echo "要更新的学生信息记录为:$stringLine" echo "输入要更改的信息(1.姓名 2.学院编号 3.说明信息(1 or 2 or 3)):" read subCommand case $subCommand in 1) echo "请输入该学生的姓名: " read newStudentName sed -i '/$studentNo/d' $DBStudentInfoPath > $DBStudentInfoPath"_back" mv $DBStudentInfoPath"_back" $DBStudentInfoPath echo "$studentNo,$newStudentName,$stuCollegeNo,$studentSay" >> $DBStudentInfoPath echo "更新学生记录成功($studentNo,$newStudentName,$stuCollegeNo,$studentSay)" ;; 2) echo "请输入该学生的学院编号: " read newCollegeNo collegeString=`grep $newCollegeNo $DBCollegeInfoPath` if [ "$collegeString" != "" ]; then { sed -i '/$studentNo/d' $DBStudentInfoPath > $DBStudentInfoPath"_back" mv $DBStudentInfoPath"_back" $DBStudentInfoPath echo "$studentNo,$newStudentName,$newCollegeNo,$studentSay" >> $DBStudentInfoPath echo "更新学生记录成功($studentNo,$newStudentName,$newCollegeNo,$studentSay)" } else { echo "此学院编号($newCollegeNo)不存在于学院记录文件中,更新失败" } fi ;; 3) echo "请输入该学生的说明信息(1.inschool 2.suspended 3.dropout):" read sayInfo case $sayInfo in 1) sed -i '/$studentNo/d' $DBStudentInfoPath > $DBStudentInfoPath"_back" mv $DBStudentInfoPath"_back" $DBStudentInfoPath echo "$studentNo,$stuName,$stuCollegeNo,inschool" >> $DBStudentInfoPath echo "更新学生记录成功($studentNo,$stuName,$stuCollegeNo,inschool)" ;; 2) sed -i '/$studentNo/d' $DBStudentInfoPath > $DBStudentInfoPath"_back" mv $DBStudentInfoPath"_back" $DBStudentInfoPath echo "$studentNo,$stuName,$stuCollegeNo,suspended" >> $DBStudentInfoPath echo "更新学生记录成功($studentNo,$stuName,$stuCollegeNo,suspended)" ;; 3) sed -i '/$studentNo/d' $DBStudentInfoPath > $DBStudentInfoPath"_back" mv $DBStudentInfoPath"_back" $DBStudentInfoPath echo "$studentNo,$stuName,$stuCollegeNo,dropout" >> $DBStudentInfoPath echo "更新学生记录成功($studentNo,$stuName,$stuCollegeNo,dropout)" ;; *) echo "说明信息填写有误,更新记录失败" ;; esac ;; *) echo "输入有误,更新失败!" ;; esac } fi } show_student_grade(){ echo "=======================每位学生的总成绩============================== " awk 'BEGIN{FS=OFS=","}{ namearrays[$1]=$2 arrays[$1]+=$4 } END{for(studentno in arrays) print studentno,namearrays[studentno],"总成绩:"arrays[studentno]}' $DBGradeInfoPath #echo "=======================总成绩的前20名============================= " #awk 'BEGIN{FS=OFS=","}{ # namearrays[$1]=$2 # arrays[$1]+=$4 #} #END{ #len=asorti(arrays,sortarrays) #for(i=0;i<=len;++i) # print sortarrays[i],"总成绩:"arrays[sortarrays[i]]}' $DBGradeInfoPath #echo "=======================每个学科的前10名============================= " #awk 'BEGIN{FS=OFS=","}{ # namearrays[$1]=$2 # objects[$3","$1]=$4 #}END{ # for(object in objects) # for (name in namearrays) # print object,name,objects[object","name] #}' $DBGradeInfoPath } show_stu_info(){ echo "==================================================" echo "学生编号:姓名:学院编号:说明信息"; cat $DBStudentInfoPath; } show_college_info(){ echo "==================================================" echo "学院编号:学院名称"; cat $DBCollegeInfoPath; } show_stugrade_info(){ echo "==================================================" echo "学生编号:学生姓名:科目名称:成绩:说明信息"; cat $DBGradeInfoPath; } while true do #三个数据文件路径的说明 echo " " echo "===============几个文件存储路径说明===============" echo "学生信息记录:/home/xiaoyaomeng/LinuxHomeWork/DBStudentInfo.txt" echo "学院信息记录:/home/xiaoyaomeng/LinuxHomeWork/DBCollegeInfo.txt" echo "成绩信息记录:/home/xiaoyaomeng/LinuxHomeWork/DBGradeInfo.txt" #DBStudentInfoPath = "/home/xiaoyaomeng/LinuxHomeWork/DBStudentInfo.txt" #DBCollegeInfoPath = "/home/xiaoyaomeng/LinuxHomeWork/DBCollegeInfo.txt" #读取用户输入的指令 echo "===============欢迎进入学生管理系统===============" echo "0.退出学生管理系统" echo "1.增加/删除学生信息记录" echo "2.增加/删除学院信息记录" echo "3.增加/删除学生成绩记录" echo "4.显示所有学生信息记录" echo "5.显示所有学院信息记录" echo "6.显示所有学生成绩记录" echo "7.修改指定学号的记录" echo "8.显示学生成绩统计结果" echo "输入command:" read iCommand case $iCommand in 0) echo "已经安全退出系统." exit 0 ;; 1) echo "1.增加学生信息记录 2.删除学生信息记录" read subCommand case $subCommand in 1) insert_student_info ;; 2) delete_student_info ;; *) echo "输入的命令有误(仅能 1 or 2)" ;; esac ;; 2) echo "1.增加学院信息记录 2.删除学院信息记录" read subCommand case $subCommand in 1) insert_college_info ;; 2) delete_college_info ;; *) echo "输入的命令有误(仅能 1 or 2)" ;; esac ;; 3) echo "1.增加学生成绩信息记录 2.删除学生成绩信息记录" read subCommand case $subCommand in 1) insert_stugrade_info ;; 2) delete_stugrade_info ;; *) echo "输入的命令有误(仅能 1 or 2)" ;; esac ;; 4) show_stu_info ;; 5) show_college_info ;; 6) show_stugrade_info ;; 7) update_student_info ;; 8) show_student_grade ;; *) echo "输入的命令不合法" ;; esac done
附带下载地址:
http://download.csdn.net/detail/u011133213/6780179