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

来源:互联网 发布:qq钓鱼软件生成器 编辑:程序博客网 时间:2024/06/16 01:21

读过大学,学过Linux的人懂得这种蛋疼的感觉.....Shell程序,蛮贴出来吧!


实验内容

Ø 作业:学生成绩管理程序

Ø 功能:

对学生成绩进行管理。要求实现数据的基本操作:学院和学生信息以及学生成绩的增加,修改,删除,统计

Ø 构造三个类似数据库的文本文件:

   第一个为学院信息文件,包含字段:

   学院编号(唯一),学院名称

   第二个为学生信息文件,包含字段:

   学号(唯一),学生姓名,所在学院编号,说明(休学suspended,退学dropout

   第三个为学生成绩文件,包含字段:

   学号(唯一),学生姓名,科目名称,成绩;说明(期考final,补考makeup

   说明:每个记录占一行;分隔符可以自己选定,建议用”,”; 编码规则自己定;文件名自己定

 

程序功能:要求实现4个功能,每个功能作为一个函数

1.向文件中插入记录

2.显示文件中的每条记录的每个字段值

3.从文件中修改指定学号的记录

4.对学生成绩进行统计(包括每个学生总成绩;每个学科前十名和总成绩前二十名统计)


截图:



#!/bin/bash#!/bin/awk -fDBStudentInfoPath="/home/xiaoyaomeng/LinuxHomeWork/DBStudentInfo.txt"DBCollegeInfoPath="/home/xiaoyaomeng/LinuxHomeWork/DBCollegeInfo.txt"DBGradeInfoPath="/home/xiaoyaomeng/LinuxHomeWork/DBGradeInfo.txt" insert_student_info(){echo "请输入学生的编号(如果文件中已存在则插入失败!): "read studentNoexport existFlag=false #判断是否存在编号和输入的一样的学生记录#cat /home/xiaoyaomeng/LinuxHomeWork/DBStudentInfo.txt | while read linewhile read linedolineStuNo=${line/,*/}#echo "数字$lineStuNo,数字2$studentNo"if [ "$lineStuNo" = "$studentNo" ];then {export existFlag=truebreak}fidone<$DBStudentInfoPath#echo "$existFlag"if [ "$existFlag" = "true" ];then echo "输入的编号$studentNo已经存在了,插入失败!"else {export existFlag=falseecho "请输入该学生的姓名:"read stuNameecho "请输入该学生所在院校编号(必须是学院信息记录文件中有的学院编号):"read stuCollegeNowhile read linedocollegeNo=${line/,*/}if [ "$collegeNo" = "$stuCollegeNo" ];then {export existFlag=truebreak}fidone<$DBCollegeInfoPathif [ "$existFlag" = "false" ];then {echo "输入的学院编号$stuCollegeNo不存在于学院信息记录文件中($DBCollegeInfoPath)"}else {echo "请输入该学生的说明信息(1.inschool 2.suspended 3.dropout):"read sayInfocase $sayInfo in 1)echo "$studentNo,$stuName,$stuCollegeNo,inschool" >> $DBStudentInfoPathecho "插入学生记录成功($studentNo,$stuName,$stuCollegeNo,inschool)";;2)echo "$studentNo,$stuName,$stuCollegeNo,suspended" >> $DBStudentInfoPathecho "插入学生记录成功($studentNo,$stuName,$stuCollegeNo,suspended)    ";;3)echo "$studentNo,$stuName,$stuCollegeNo,dropout" >> $DBStudentInfoPathecho "插入学生记录成功($studentNo,$stuName,$stuCollegeNo,dropout)    ";;*)echo "说明信息填写有误,插入记录失败";;esac}fi}fi}delete_student_info(){echo "请输入要删除的学生编号(若学生信息记录文件中不存在,则删除失败): "read studentNoexport nowline=0;export delCount=0;while read linedoexport nowline=$(($nowline + 1))echo $line":"$nowlinelineStudentNo=${line/,*/}if [ "$lineStudentNo" = "$studentNo" ];then {echo $nowline "是当前行"sed "$nowline"d $DBStudentInfoPath > $DBStudentInfoPath"_back"export delCount=$(($delCount + 1))mv $DBStudentInfoPath"_back" $DBStudentInfoPath}fidone<$DBStudentInfoPath#mv $DBStudentInfoPath"_back" $DBStudentInfoPathecho "删除操作处理结束: 被删除的学生编号为$studentNo,删除的学生个数为$delCount"}insert_college_info(){echo "请输入要加入的学院编号(若学院记录文件中存在,则插入失败):"read collegeNoexport existFlag=falsewhile read linedolineCollegeNo=${line/,*/}if [ "$lineCollegeNo" = "$collegeNo" ];then {export existFlag=truebreak}fidone<$DBCollegeInfoPathif [ "$existFlag" == "true" ];then {export existFlag=falseecho "学院记录文件中已经存在此学院编号记录了,插入失败"}else {echo "请输入学院的名称: "read collegeNameecho "$collegeNo,$collegeName" >> $DBCollegeInfoPathecho "学院记录插入成功,插入的记录为($collegeNo,$collegeName)"}fi}delete_college_info(){echo "请输入要删除的学院编号(若学院记录文件中不存在则删除失败): "read collegeNoexport nowline=0;export delCount=0;while read linedo export nowline=$(($nowline + 1))nowCollegeNo=${line/,*/}if [ "$nowCollegeNo" = "$collegeNo" ];then {sed "$nowline"d $DBCollegeInfoPath > $DBCollegeInfoPath"_back"export delCount=$(($delCount + 1))mv $DBCollegeInfoPath"_back" $DBCollegeInfoPath}fidone<$DBCollegeInfoPath#mv $DBCollegeInfoPath"_back" $DBCollegeInfoPathecho "删除学院记录完成:删除的学院编号为$collegeNo,删除的个数为$delCount"}insert_stugrade_info(){echo "请输入该成绩对应的学生编号(若学生记录文件中不存在该编号则插入失败): "read studentNoexport existFlag=falseexport nowline=0;export stuName=""while read linedolineStudentNo=${line/,*/}nowline=$(($nowline + 1))if [ "$lineStudentNo" = "$studentNo" ];then {export existFlag=true#如果是有这个学生编号的话,把该学生的姓名取出来export stuName=`echo "$line"|awk -F',' '{print $2}'` #echo $stuNamebreak}fidone<$DBStudentInfoPathif [ "$existFlag" = "false" ];then {echo "sorry,学生记录文件中不存在学生编号为$studentNo,插入失败"}else {export existFlag=falseecho "请输入该成绩对应的科目名称: "read objectNameecho "请输入该成绩的分数(0<=grade<=100): "read gradeResultif [ $gradeResult -le 100 ] && [ $gradeResult -ge 0 ];then {#echo $gradeResultecho "请输入该成绩的说明信息(1.期考final  2.补考makeup)"read sayInfocase $sayInfo in1)echo "$studentNo,$stuName,$objectName,$gradeResult,final" >> $DBGradeInfoPathecho "插入成功($studentNo,$stuName,$objectName,$gradeResult,final)";;2)echo "$studentNo,$stuName,$objectName,$gradeResult,makeup" >> $DBGradeInfoPathecho "插入成功($studentNo,$stuName,$objectName,$gradeResult,makeup)";;*)echo "输入有误(仅能选1 or 2),插入失败";;esac}else {echo "输入的成绩不符合要求(0<=grade<=100),插入失败"}fi}fi}delete_stugrade_info(){echo "输入要删除成绩的学生的编号(存在于学生记录文件): "read studentNoexport existFlag=falseexport delCount=0;export delLine=0;while read linedolineStudentNo=${line/,*/}if [ "$lineStudentNo" = "$studentNo" ];then {export existFlag=truebreak}fidone<$DBStudentInfoPathif [ "$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<$DBGradeInfoPathsed -i '/$studentNo/d' $DBGradeInfoPath > $DBGradeInfoPath"_back"mv $DBGradeInfoPath"_back" $DBGradeInfoPathecho "删除学生成绩成功:被删除学生编号$studentNo"}else {echo "学生记录文件中不存在该学生编号$studentNo,删除失败"}fi}update_student_info(){echo "请输入要修改的学生编号(默认仅可修改学生自己的信息):"read studentNostringLine=`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 subCommandcase $subCommand in1)echo "请输入该学生的姓名: "read newStudentNamesed -i '/$studentNo/d' $DBStudentInfoPath > $DBStudentInfoPath"_back"mv $DBStudentInfoPath"_back" $DBStudentInfoPathecho "$studentNo,$newStudentName,$stuCollegeNo,$studentSay" >> $DBStudentInfoPathecho "更新学生记录成功($studentNo,$newStudentName,$stuCollegeNo,$studentSay)";;2)echo "请输入该学生的学院编号: "read newCollegeNocollegeString=`grep $newCollegeNo $DBCollegeInfoPath`if [ "$collegeString" != "" ];then {sed -i '/$studentNo/d' $DBStudentInfoPath > $DBStudentInfoPath"_back"mv $DBStudentInfoPath"_back" $DBStudentInfoPathecho "$studentNo,$newStudentName,$newCollegeNo,$studentSay" >> $DBStudentInfoPathecho "更新学生记录成功($studentNo,$newStudentName,$newCollegeNo,$studentSay)"}else {echo "此学院编号($newCollegeNo)不存在于学院记录文件中,更新失败"}fi;;3)echo "请输入该学生的说明信息(1.inschool 2.suspended 3.dropout):"read sayInfocase $sayInfo in 1)sed -i '/$studentNo/d' $DBStudentInfoPath > $DBStudentInfoPath"_back"mv $DBStudentInfoPath"_back" $DBStudentInfoPathecho "$studentNo,$stuName,$stuCollegeNo,inschool" >> $DBStudentInfoPathecho "更新学生记录成功($studentNo,$stuName,$stuCollegeNo,inschool)";;2)sed -i '/$studentNo/d' $DBStudentInfoPath > $DBStudentInfoPath"_back"mv $DBStudentInfoPath"_back" $DBStudentInfoPathecho "$studentNo,$stuName,$stuCollegeNo,suspended" >> $DBStudentInfoPathecho "更新学生记录成功($studentNo,$stuName,$stuCollegeNo,suspended)";;3)sed -i '/$studentNo/d' $DBStudentInfoPath > $DBStudentInfoPath"_back"mv $DBStudentInfoPath"_back" $DBStudentInfoPathecho "$studentNo,$stuName,$stuCollegeNo,dropout" >> $DBStudentInfoPathecho "更新学生记录成功($studentNo,$stuName,$stuCollegeNo,dropout)";;*)echo "说明信息填写有误,更新记录失败";;esac;;*)echo "输入有误,更新失败!";;esac }fi}show_student_grade(){echo "=======================每位学生的总成绩============================== "awk 'BEGIN{FS=OFS=","}{namearrays[$1]=$2arrays[$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 truedo#三个数据文件路径的说明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 iCommandcase $iCommand in0)echo "已经安全退出系统."exit 0;;1)echo "1.增加学生信息记录  2.删除学生信息记录"read subCommandcase $subCommand in 1)insert_student_info;;2)delete_student_info;;*)echo "输入的命令有误(仅能 1 or 2)";;esac;;2)echo "1.增加学院信息记录 2.删除学院信息记录"read subCommandcase $subCommand in1)insert_college_info;;2)delete_college_info;;*)echo "输入的命令有误(仅能 1 or 2)";;esac ;;3)echo "1.增加学生成绩信息记录 2.删除学生成绩信息记录"read subCommandcase $subCommand in1)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 "输入的命令不合法";;esacdone

附带下载地址:

http://download.csdn.net/detail/u011133213/6780179


3 1
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 一道中文不卡视频 中国中文字幕无线观看局域网 学妹开庖处疼流泪小说下载 学妹开庖处疼流泪视频网站 学生的母亲字幕中文翻译视频 中文不卡视频在线播放复古 学妹开庖处疼流泪视频 视频g 学妹开庖处疼流泪t 学妹开庖处疼流泪图新闻 学妹开庖处疼流泪漫画小说 学妹开庖处疼流泪高清 学妹开庖处疼流泪网站 039 年轻的老师3中文字中幕中国人 学妹开庖处疼流泪t小说 学妹开庖处疼流泪视频 视频中国 学妹开庖处疼流泪音频 中文字字视频人人 学妹开庖处疼流泪小说txt 学妹开庖处疼流泪视频 手机 视频字幕中文翻译 五姑娘中文视频版 小护土叫得太完整视频中文 上了瘾中文版视频 按摩院里的中国姑娘电影 五姑娘视频网搜 架起乳白腿一深一浅 9老师啪国线自产 老师你的水又多又甜视频 91国偷自产短视频网站 在卫生间里就做的视频 家庭毋HH伦s线播放中文字版 视频精二区 中文字 美国式 柰忌轧伦中文 二十三又嫩水又多 家庭毋HH伦s中文字幕 毋HH伦中文字幕视频 中文字 幕278页 子母相伦中文字幕视频 91国视频产产线动物 日本中文字幕1000视频 高清视频中文字幕