这篇是对前面五篇代码的整理。
(1)http://blog.csdn.net/wjf_1997/article/details/78474118
(2)http://blog.csdn.net/wjf_1997/article/details/78502039
(3)http://blog.csdn.net/wjf_1997/article/details/78513086
(4)http://blog.csdn.net/wjf_1997/article/details/78513201
(5)http://blog.csdn.net/wjf_1997/article/details/78513267
代码
#!/bin/bash
#!/bin/awk -f
#建立存储路径
DBCollegeInfoPath="./DBCollegeInfo.txt"
DBStudentInfoPath="./DBStudentInfo.txt"
DBStudentGradeInfoPath="./DBStudentGradeInfo.txt"
#一、显示功能
show_college_info(){
echo "================================================================="
echo "学院编号,学院名称"
cat $DBCollegeInfoPath
}
show_stu_info(){
echo "================================================================="
echo "学生编号,学生姓名,学院编号,说明信息"
cat $DBStudentInfoPath
}
show_stugrade_info(){
echo "================================================================="
echo "学生编号,学生姓名,科目名称,成绩,说明信息"
cat $DBStudentGradeInfoPath
}
show_stu_grade_info(){
echo "======================总成绩(相同专业)============================"
awk 'BEGIN{FS=OFS=","}{
namearrays[$1]=$2
arrays[$1]+=$4
}
END{for(studentno in arrays)
print studentno,namearrays[studentno],"总成绩:"arrays[studentno]}' $DBStudentGradeInfoPath
echo "======================学科排名10(相同专业)============================"
echo "名次 姓名 总成绩"
for course in $(cut -d, -f3 $DBStudentGradeInfoPath |sort -n | uniq )
do
echo "$course:"
grep "$course" $DBStudentGradeInfoPath | sort -t, -k4 -n -r|
awk 'BEGIN{FS=","}{if(NR<=10)print NR "\t" $2 "\t" $4}'
echo ""
done
echo "=======================总成绩排名20(相同专业)=========================="
echo "名次 姓名 总成绩"
awk 'BEGIN{FS=","}{arr[$2]+=$4}
END{for(i in arr)print i "," arr[i]}' $DBStudentGradeInfoPath |sort -t, -k2 -n -r|
awk 'BEGIN{FS=","}{if(NR<=20)print NR "\t" $1 "\t" $2}'
echo ""
}
#二、增加功能
insert_college_info(){
show_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 exitFlag=false
echo "此学院已经存在,插入失败!"
}
else {
echo "请输入学院的名称:"
read collegeName
echo "$collegeNo,$collegeName" >> $DBCollegeInfoPath
echo "学院记录插入成功!"
show_college_info;
}
fi
}
insert_stu_info(){
show_stu_info;
echo "请输入要添加的学生编号(若存在,则插入失败!)"
read studentNo
export existFlag=false
while read line
do
lineStuNo=${line/,*/}
if [ "$lineStuNo" = "$studentNo" ]; then
{
export existFlag=true
break
}
fi
done<$DBStudentInfoPath
if [ "$existFlag" = "true" ]; then
echo "输入的学生编号已经存在,插入失败!"
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 "输入的学院编号不存在!"
}
else {
echo "请输入该学生的说明信息( 1.在校inschool 2.休学suspend 3.辍学dropout ):"
read choice
case $choice in
"1")
echo "$studentNo,$stuName,$stuCollegeNo,inschool" >> $DBStudentInfoPath
echo "学生记录插入成功!"
show_stu_info;
;;
"2")
echo "$studentNo,$stuName,$stuCollegeNo,suspend" >> $DBStudentInfoPath
echo "学生记录插入成功!"
show_stu_info;
;;
"3")
echo "$studentNo,$stuName,$stuCollegeNo,dropout" >> $DBStudentInfoPath
echo "学生记录插入成功!"
show_stu_info;
;;
*)
echo "Usage:$0{1|2|3}"
echo "学生记录插入失败!"
show_stu_info;
;;
esac
}
fi
}
fi
}
insert_stugrade_info(){
show_stugrade_info;
echo "请输入要添加的学生编号(若不存在,则插入失败!):"
read studentNo
export existFlag=false
export nowline=0;
export stuName=""
while read line
do
lineStudentNo=${line/,*/}
export nowline=$(($nowline+1))
if [ "$lineStudentNo" = "$studentNo" ]; then
{
export existFlag=true
export stuName=`echo "$line" | awk -F',' '{print $2}'`
break
}
fi
done<$DBStudentInfoPath
if [ "$existFlag" = "false" ]; then
{
echo "学生记录中没有这个学生,插入失败!"
}
else {
export existFlag=false
echo "请科目名称:"
read objectName
echo "请输入科目成绩(0-100):"
read gradeResult
if [ $gradeResult -le 100 ] && [ $gradeResult -ge 0 ]; then
{
echo "请输入该成绩的说明信息( 1.期末final 2.补考makeup ):"
read choice
case $choice in
"1")
echo "$studentNo,$stuName,$objectName,$gradeResult,final" >> $DBStudentGradeInfoPath
echo "插入成功!"
show_stugrade_info;
;;
"2")
echo "$studentNo,$stuName,$objectName,$gradeResult,makeup" >> $DBStudentGradeInfoPath
echo "插入成功!"
show_stugrade_info;
;;
*)
echo "Usage:$0{1|2}"
echo "插入失败!"
show_stugrade_info;
;;
esac
}
else {
echo "输入成绩不符合要求,插入失败!"
}
fi
}
fi
}
#三、删除功能
delete_college_info(){
show_college_info;
echo "请输入要删除的学院的编号(若不存在,删除失败!):"
read collegeNo
export nowLine=0;
while read line
do
export nowLine=$(($nowLine+1))
nowCollegeNo=${line/,*/}
if [ "$nowCollegeNo" = "$collegeNo" ]; then
{
sed "$nowLine"d $DBCollegeInfoPath > $DBCollegeInfoPath"_back"
mv $DBCollegeInfoPath"_back" $DBCollegeInfoPath
echo "删除成功!"
}
else
{
echo "删除失败!"
}
fi
done<$DBCollegeInfoPath
show_college_info;
}
delete_stu_info(){
show_stu_info;
echo "请输出要删除学生的编号(若不存在,删除失败!):"
read studentNo
export nowLine=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
echo "删除成功!"
}
else
{
echo "删除失败!"
}
fi
done<$DBStudentInfoPath
show_stu_info;
}
delete_stugrade_info(){
show_stugrade_info;
echo "请输出要删除学生的编号(若不存在,删除失败!):"
read studentNo
#export existFlag=false
export nowLine=0;
while read line
do
export nowLine=$(($nowLine+1))
lineStudentNo=${line/,*/}
if [ "$lineStudentNo" = "$studentNo" ]; then
{
sed "$nowLine"d $DBStudentGradeInfoPath > $DBStudentGradeInfoPath"_back"
mv $DBStudentGradeInfoPath"_back" $DBStudentGradeInfoPath
echo "删除成功!"
}
else
{
echo "删除失败!"
}
fi
done<$DBStudentGradeInfoPath
#while read line
#do
# lineStudentNo=${line/,*/}
#if [ "$lineStudentNo" = "$studentNo" ]; then
#{
# export existFlag=true
# break
#}
#fi
# done<$DBStudentInfoPath
#if [ "$existFlag" = "true" ]; then
#{
# export existFlag=false
# sed -i '/$studentNo/d' $DBStudentGradeInfoPath > $DBStudentGradeInfoPath"_back"
# mv $DBStudentGradeInfoPath"_back" $DBStudentGradeInfoPath
# echo "删除成功!"
# show_stugrade_info;
#}
#else {
# echo "删除失败!"
#}
show_stugrade_info;
}
#四、修改功能
update_stuName_info(){
show_stu_info;
echo "请输入要修改信息的学生编号(默认仅可以修改自己的信息):"
read studentNo
export nowLine=0;
stringLine=`grep $studentNo $DBStudentInfoPath`
stuName=`echo "$stringLine" | awk -F ',' '{print $2}'`
stuCollegeNo=`echo "$stringLine" | awk -F ',' '{print $3}'`
studentSay=`echo "$stringLine" | awk -F ',' '{print $4}'`
echo "要更新的学生信息记录为:$stringLine"
echo "请输入新的名字:"
read newStudentName
while read line
do
lineStudentNo=${line/,*/}
export nowLine=$(($nowLine+1))
if [ "$lineStudentNo" = "$studentNo" ]; then
{
sed "$nowLine"d $DBStudentInfoPath > $DBStudentInfoPath"_back"
mv $DBStudentInfoPath"_back" $DBStudentInfoPath
echo "$studentNo,$newStudentName,$stuCollegeNo,$studentSay" >> $DBStudentInfoPath
echo "更新成功!"
show_stu_info;
}
fi
done<$DBStudentInfoPath
}
update_stuCollege_info(){
show_stu_info;
echo "请输入要修改信息的学生编号(默认仅可以修改自己的信息):"
read studentNo
export nowLine=0;
stringLine=`grep $studentNo $DBStudentInfoPath`
stuName=`echo "$stringLine" | awk -F ',' '{print $2}'`
stuCollegeNo=`echo "$stringLine" | awk -F ',' '{print $3}'`
studentSay=`echo "$stringLine" | awk -F ',' '{print $4}'`
echo "要更新的学生信息记录为:$stringLine"
echo "请输入新学院编号:"
read newstuCollegeNo
while read line
do
lineStudentNo=${line/,*/}
export nowLine=$(($nowLine+1))
if [ "$lineStudentNo" = "$studentNo" ]; then
{
sed "$nowLine"d $DBStudentInfoPath > $DBStudentInfoPath"_back"
mv $DBStudentInfoPath"_back" $DBStudentInfoPath
echo "$studentNo,$stuName,$newstuCollegeNo,$studentSay" >> $DBStudentInfoPath
echo "更新成功!"
show_stu_info;
}
fi
done<$DBStudentInfoPath
}
#五、查找功能
find_stu_info(){
echo "请输入学生的姓名:"
read stuName
stringLine_one=`grep $stuName $DBStudentInfoPath`
stringLine_two=`grep $stuName $DBStudentGradeInfoPath`
echo "查询的学生信息:"
echo "$stringLine_one"
echo "$stringLine_two"
show_stu_info;
show_stugrade_info;
}
while true
do
echo "=========================学生信息管理系统========================="
echo "0.退出"
echo "1.显示"
echo "2.增加"
echo "3.删除"
echo "4.修改"
echo "5.查找"
echo "输入choice:"
read choice
case $choice in
#零、退出
"0")
echo "退出!"
exit 0
;;
#一、显示(show)
"1")
echo "1.显示所有学院信息"
echo "2.显示所有学生信息"
echo "3.显示所有学生成绩信息"
echo "4.显示学生成绩统计结果"
echo "输入choice:"
read choice
case $choice in
"1")
show_college_info
;;
"2")
show_stu_info
;;
"3")
show_stugrade_info
;;
"4")
show_stu_grade_info
;;
*)
echo "Usage:$0{1|2|3|4}"
;;
esac ;;
#二、增加(insert)
"2")
echo "1.增加学院信息"
echo "2.增加学生信息"
echo "3.增加学生成绩信息"
echo "输入choice"
read choice
case $choice in
"1")
insert_college_info
;;
"2")
insert_stu_info
;;
"3")
insert_stugrade_info
;;
*)
echo "Usage:$0{1|2|3}"
esac ;;
#三、删除(delete)
"3")
echo "1.删除学院信息"
echo "2.删除学生信息"
echo "3.删除学生成绩信息"
echo "输入choice"
read choice
case $choice in
"1")
delete_college_info
;;
"2")
delete_stu_info
;;
"3")
delete_stugrade_info
;;
*)
echo "Usage:$0{1|2|3}"
esac ;;
#四、修改(update)
"4")
echo "1.修改学生的姓名"
echo "2.修改学院编号"
echo "输入choice"
read choice
case $choice in
"1")
update_stuName_info
;;
"2")
update_stuCollege_info
;;
*)
echo "Usage:$0{1|2}"
esac ;;
#五、查找(find)
"5")
find_stu_info
;;
*)
echo "Usage:$0{0|1|2|3|4|5}"
;;
esac
done