#!/bin/bash# 學生管理系統# @version 1.0 #設置全局變數##學生信息和學院信息#學院相關變數school_num=""school_name="" #學生相關變數stu_num=""stu_name=""stu_des="" #成績相關變數course=""score="" ...
#該管理系統是參考兩位博主(時間有點遠了,我忘了,請博主看到後聯繫我)後自行修改添加的。登錄過程還有很多不完善,我就拋磚引玉啦。
廢話不多,直接上碼!
#!/bin/bash
# 學生管理系統
# @version 1.0
#設置全局變數
##學生信息和學院信息
#學院相關變數
school_num=""
school_name=""
#學生相關變數
stu_num=""
stu_name=""
stu_des=""
#成績相關變數
course=""
score=""
score_des=""
#用戶賬戶信息
user_name=""
user_passwd=""
##數據文件存放路徑
source_dir="./source"
schools_db="./source/schools.db"
students_db="./source/students.db"
scores_db="./source/scores.db"
user_db="./source/user.db"
#創建數據文件
mkdir_sourcedir(){
if [ ! -d $source_dir ];then
mkdir -p $source_dir
fi
if [ ! -f $schools_db ];then
touch $schools_db
fi
if [ ! -f $students_db ];then
touch $students_db
fi
if [ ! -f $scores_db ];then
touch $scores_db
fi
if [ ! -f $user_db ];then
touch $user_db
fi
}
create_user(){
read -p "請輸入你的用戶名:" user_name
cat file | grep $user_name &>/dev/null
[ $? -eq 0 ] && echo "這個用戶已經存在,請重新創建!" && create_user
if [ -z $user_name ];then
echo "該用戶可創建" && create_user
fi
read -p "請輸入密碼:" user_passwd
read -p "請再次輸入密碼:" passwd1
if [[ "$user_passwd" != "$passwd1" ]] ;then
echo "密碼驗證不正確,請重新創建" && create_user
fi
if [ ! -z "$user_passwd" ];then
echo "$user_name "$user_passwd" " >>file
echo "創建成功!,按任意鍵將跳轉到登錄界面..."
fi
if [ -z "$user_passwd" ];then
echo "$user_name " " " >>file
echo "創建成功!,按任意鍵將跳轉到登錄界面..."
fi
clear
read choice
case $choice in
*)
login
;;
esac
}
login(){
read -p "請輸入用戶名:" user_name
a="`cat file | awk '$1=="'$user_name'"'|cut -d ' ' -f2-`"
b=`echo "$a" | sed 's/ //g'`
cat file | awk '$1=="'$user_name'"' &> /dev/null
if [ $? -eq 0 ];then
read -p "請輸入你的密碼:" user_passwd
c=`echo $user_passwd | sed 's/ //g'`
if [[ "$b" = "$c" ]];then
echo "登錄成功!"
else
echo "密碼驗證不正確,請選擇 1. 重新創建 2. 重新登錄 3. 退出系統"
clear
read choice
case $choice in
1)
create_user
;;
2)
login
;;
3)
exit 0
;;
esac
fi
else
echo "該用戶不存在,請選擇 1. 重新創建 2. 重新登錄 3. 退出系統"
#create_user
clear
read choice
case $choice in
1)
create_user
;;
2)
login
;;
3)
exit 0
;;
esac
fi
Welcome
}
haha(){
case $main in
1)
create_user
;;
2)
login
;;
3)
exit 0
;;
esac
}
cat << EOF
************請選擇:**************
1) 創建用戶
2) 直接登錄
3) 退出
EOF
read -p "Choice:" main
# 主函數
Welcome()
{
# reset
mkdir_sourcedir #數據文件不存在則創建
echo -e "\t\t\t--------------------------"
echo -e "\t\t\t 歡迎進入學生管理系統"
echo -e "\t\t\t--------------------------"
echo -e "\t\t\t 按任意鍵繼續……"
read choice
case $choice in
*)
Module
;;
esac
}
#模塊選擇
Module(){
clear
echo -e "\t\t\t**********************"
echo -e "\t\t\t請輸入對應數字選擇信息模塊"
echo -e "\t\t\t 1. 學院信息管理"
echo -e "\t\t\t 2. 學生信息管理"
echo -e "\t\t\t 3. 成績信息管理"
echo -e "\t\t\t 4. 退出系統"
read choice
case $choice in
1)
Menu1
;;
2)
Menu2
;;
3)
Menu3
;;
4)
exit 0
;;
*)
Module
;;
esac
}
###############
#學院信息模塊功能實現
###############
#學院信息模塊菜單
Menu1(){
clear
echo -e "\t\t\t****************************"
echo -e "\t\t\t請輸入對應數字選擇功能"
echo -e "\t\t\t 1. 添加學院"
echo -e "\t\t\t 2. 刪除學院"
echo -e "\t\t\t 3. 學院列表"
echo -e "\t\t\t 4. 查找學院"
echo -e "\t\t\t 5. 修改學院"
echo -e "\t\t\t 6. 返回上級"
echo -e "\t\t\t 7. 退出系統"
echo -e "\t\t\t****************************"
echo "請選擇:"
read choice
case $choice in
1)
echo -e "\t\t\t添加學院"
add_school
;;
2)
echo -e "\t\t\t刪除學院"
del_school
;;
3)
echo -e "\t\t\t學院列表"
display_schools
;;
4)
echo -e "\t\t\t查找學院"
find_school
;;
5)
echo -e "\t\t\t修改學院"
update_school
;;
6)
echo -e "\t\t\t返回上級"
Module
;;
7)
exit 0
;;
*)
Menu1
;;
esac
echo "--------------------------"
echo "按任意鍵繼續……"
read c
Menu1
}
#1.添加學院信息
add_school(){
read -p "school_num:" school_num
#防止輸入空格
while [ "$school_num" = '' ];do
read -p "輸入的學院編號不能為空,請重新輸入學院編號:" school_num
done
while grep -w "$school_num" "$schools_db" > /dev/null ;do
echo "已經存在該編號的學院了,請改用其他編號!"
read -p "school_num:" school_num
done
read -p "school_name:" school_name
#防止輸入空格
while [ "$school_name" = '' ];do
read -p "輸入的學院名稱不能為空,請重新輸入學院名稱:" school_name
done
echo -e "$school_num,\t$school_name,\r" >> $schools_db
}
#2.刪除學院
del_school(){
read -p "請輸入學院的編號" school_num
while ! grep -w "$school_num" "$schools_db" > /dev/null ; do
echo "不存在該編號的學校,請輸入編號存在的學校編號: "
read -p "school_num:" school_num
done
echo "該學院的信息為:"
grep "^$school_num" $schools_db
read -p "確定刪除嗎?[yes|no]" choice
case "$choice" in
yes)
sed -i -e "/^$school_num/d" $schools_db
if [ "$?" -eq 0 ];then
echo "刪除成功"
else
echo "不存在該條記錄或者刪除失敗"
fi
;;
no)
:
;;
*)
echo "請輸入[yes|no]"
del_school
;;
esac
}
#3.展示全部學院信息
display_schools(){
echo -e "編號\t學院\t"
cat $schools_db
}
#4.查找學院
find_school(){
echo "--------------------------"
echo "輸入要查詢的學院編號"
read school_num
echo "--------------------------"
echo -e "編號\t學院\t"
grep -w $school_num $schools_db #-w只顯示全字元合的列
echo "--------------------------"
echo "查詢成功"
}
#5.修改學院信息
update_school(){
echo "--------------------------"
read -p "輸入學院編號:" school_num
#防止輸入空格
while [ "$school_num" = '' ];do
read -p "輸入有誤,請重新。輸入學院編號:" school_num
done
while ! grep -w "$school_num" "$schools_db" > /dev/null ; do
echo "不存在該編號的學校,請輸入編號存在的學校編號: "
read -p "school_num:" school_num
#防止輸入空格
while [ "$school_num" = '' ];do
read -p "輸入有誤,請重新。輸入學院編號:" school_num
done
done
echo "--------------------------"
echo -e "編號\t學院\t"
grep -w $school_num $schools_db
echo "--------------------------"
read -p "要修改的欄位信息" str1
#防止輸入空格
while [ "$str1" = '' ];do
read -p "輸入有誤,請重新。要修改的欄位信息:" str1
done
while ! grep -w "$school_num,.*," "$schools_db"|grep -w "$str1"> /dev/null;do
echo "不存在該欄位,請重新輸入:"
read str1
#防止輸入空格
while [ "$str1" = '' ];do
read -p "輸入有誤,請重新。要修改的欄位信息:" str1
done
done
read -p "修改後的欄位信息" str2
#防止輸入空格
while [ "$str2" = '' ];do
read -p "輸入有誤,請重新。修改後的欄位信息:" str2
done
read -p "確定刪除嗎?[yes|no]" choice
case "$choice" in
yes)
sed -i "/${school_num#,}/ s/$str1/$str2/" $schools_db
if [ "$?" -eq 0 ];then
echo "修改成功"
echo "--------------------------"
echo "修改後的學院信息"
echo -e "編號\t學院\t"
cat $schools_db
if [ "$str1" = "$school_num" ];then #如果修改的是學院編號還要修改學生表裡的學院編號
sed -i "/${school_num#,}/ s/$str1/$str2/" $students_db
fi
else
echo "修改失敗"
fi
;;
no)
:
;;
*)
echo "請輸入[yes|no]"
del_student
;;
esac
}
###################
#學生信息模塊功能實現
###################
#學生信息模塊菜單
Menu2() {
clear
echo -e "\t\t\t**********************"
echo -e "\t\t\t請輸入對應數字選擇功能"
echo -e "\t\t\t 1. 添加學生"
echo -e "\t\t\t 2. 刪除學生"
echo -e "\t\t\t 3. 學生列表"
echo -e "\t\t\t 4. 查找學生"
echo -e "\t\t\t 5. 修改學生"
echo -e "\t\t\t 6. 返回上級"
echo -e "\t\t\t 7. 退出系統"
echo -e "\t\t\t**********************"
echo "請選擇:"
read choice
case $choice in
1)
echo -e "\t\t\t添加學生信息"
add_student
;;
2)
echo -e "\t\t\t刪除學生信息"
del_student
;;
3)
echo -e "\t\t\t展示學生信息"
display_students
;;
4)
echo -e "\t\t\t搜索學生信息"
search_student_num
;;
5)
echo -e "\t\t\t修改學生信息"
modify_student
;;
6)
echo -e "\t\t\t返回上一個頁面"
Module
;;
7)
exit 0
;;
*)
Menu2
;;
esac
echo "--------------------------"
echo "按任意鍵繼續……"
read c
Menu2
}
#1.添加學生信息
add_student(){
read -p "stu_num:" stu_num
while grep -w "$stu_num" "$students_db" > /dev/null ;do
echo "已經存在該學號的學生了,請改用其他學號!"
read -p "stu_num:" stu_num
done
read -p "stu_name:" stu_name
read -p "school_num:" school_num
#將結果丟失,不顯示在屏幕中
while ! grep -w "$school_num" "$schools_db" > /dev/null ; do
echo "不存在該編號的學校,請輸入編號存在的學校編號: "
read -p "school_num:" school_num
done
read -p "stu_des:" stu_des
echo -e "$stu_num,\t$stu_name,\t$school_num,\t$stu_des,\r" >> $students_db
echo "成功添加學生信息,添加的信息為: "
grep -w $stu_num $students_db
}
#2.刪除學生
del_student(){
read -p "請輸入要刪除的學生的學號" num
echo "該學生的信息為:"
grep "^$num" $students_db
read -p "確定刪除嗎?[yes|no]" choice
case "$choice" in
yes)
sed -i -e "/^$num/d" $students_db
if [ "$?" -eq 0 ];then
echo "刪除成功"
else
echo "不存在該條記錄或者刪除失敗"
fi
;;
no)
:
;;
*)
echo "請輸入[yes|no]"
del_student
;;
esac
}
#3.顯示所有學生信息
display_students() {
echo "--------------------------"
echo -e "學號\t姓名\t學院\t說明"
sed -n '1,$p' $students_db
echo "--------------------------"
echo -e "\r"
}
#4.查詢學生信息
search_student_num() {
echo "--------------------------"
echo "輸入要查詢的學號"
read num
echo "--------------------------"
echo -e "學號\t姓名\t學院\t說明"
grep -w $num $students_db
# grep -w "${num#*,}" $students_db
echo "--------------------------"
echo "查詢成功"
}
#5.修改學生信息
modify_student() {
echo "--------------------------"
echo "輸入學生所在的學院:"
read school_num
#防止輸入空格
while [ "$school_num" = '' ];do
read -p "輸入有誤,請重新。輸入學生所在的學院:" school_num
done
while ! grep -w "$school_num" "$schools_db" > /dev/null;do
echo "不存在該編號的學院,請重新輸入:"
read school_num
#防止輸入空格
while [ "$school_num" = '' ];do
read -p "輸入有誤,請重新。輸入學生所在的學院:" school_num
done
done
echo "輸入要查詢的學號:"
read num
#防止輸入空格
while [ "$num" = '' ];do
read -p "輸入有誤,請重新。輸入要查詢的學號:" num
done
while ! grep -w "$num,.*,.*,.*," "$students_db" > /dev/null;do
echo "不存在該編號的學生,請重新輸入:"
read num
#防止輸入空格
while [ "$num" = '' ];do
read -p "輸入有誤,請重新。輸入要查詢的學號:" num
done
done
echo "--------------------------"
echo -e "學號\t姓名\t學院\t說明"
grep "${num#,}" $students_db
echo "--------------------------"
echo "要修改的欄位信息"
read str1
#防止輸入空格
while [ "$str1" = '' ];do
read -p "輸入有誤,請重新。要修改的欄位信息:" str1
done
while ! grep -w "$num,.*,.*,.*," "$students_db"|grep -w "$str1"> /dev/null;do
echo "不存在該欄位,請重新輸入:"
read str1
#防止輸入空格
while [ "$str1" = '' ];do
read -p "輸入有誤,請重新。要修改的欄位信息:" str1
done
done
echo "修改後的欄位信息"
read str2
#防止輸入空格
while [ "$str2" = '' ];do
read -p "輸入有誤,請重新。修改後的欄位信息:" str2
done
if [ "$str1" = "$school_num" ];then
while ! grep -w "$str2" "$schools_db" > /dev/null;do
echo "不存在該編號的學院,請重新輸入:"
read str2
#防止輸入空格
while [ "$str2" = '' ];do
read -p "輸入有誤,請重新。修改後的欄位信息:" str2
done
done
fi
read -p "確定刪除嗎?[yes|no]" choice
case "$choice" in
yes)
#如果修改的是學生編號還要修改成績表裡的學生編號
if [ "$str1" = "$num" ];then
sed -i "/${num#,}/ s/$str1/$str2/" $scores_db
fi
#如果修改的是學生姓名還要修改成績表裡的學生姓名
studentName=$(grep "^$num," "$students_db"|sed -n "1p"|cut -d "," -f 2|cut -f 2) #獲取學生的姓名,最後一個cut作用是去除字元串前的製表符
if [ "$str1" = "$studentName" ];then
sed -i "/^$num,/ s/$studentName/$str2/" "$scores_db" #根據學號來修改成績表姓名的信息
fi
#修改學生表
sed -i "/${num#,}/ s/$str1/$str2/" $students_db
if [ "$?" -eq 0 ];then
echo "------------修改成功--------------"
echo "修改後的學生信息"
echo -e "學號\t姓名\t學院\t說明"
grep "${num#,}" $students_db | grep "$str2"
else
echo "修改失敗"
fi
;;
no)
:
;;
*)
echo "請輸入[yes|no]"
modify_student
;;
esac
}
###################
#成績信息模塊功能實現
###################
#成績信息模塊菜單
Menu3() {
clear
echo -e "\t\t\t**********************"
echo -e "\t\t\t請輸入對應數字選擇功能"
echo -e "\t\t\t 1. 添加成績"
echo -e "\t\t\t 2. 刪除成績"
echo -e "\t\t\t 3. 成績列表"
echo -e "\t\t\t 4. 查找成績"
echo -e "\t\t\t 5. 修改成績"
echo -e "\t\t\t 6. 成績統計"
echo -e "\t\t\t 7. 返回上級"
echo -e "\t\t\t 8. 退出系統"
echo -e "\t\t\t**********************"
echo "請選擇:"
read choice
case $choice in
1)
echo -e "\t\t\t添加成績"
add_score
;;
2)
echo -e "\t\t\t刪除成績"
del_score
;;
3)
echo -e "\t\t\t展示成績信息"
display_scores
;;
4)
echo -e "\t\t\t搜索成績信息"
search_score_num
;;
5)
echo -e "\t\t\t修改成績信息"
modify_score
;;
6)
echo -e "\t\t\t成績統計"
score_count
;;
7)
echo -e "\t\t\t返回上一個頁面"
Module
;;
8)
exit 0
;;
*)
Menu3
;;
esac
echo "--------------------------"
echo "按任意鍵繼續……"
read c
Menu3
}
#成績統計模塊
score_count(){
clear
echo -e "\t\t\t**********************"
echo -e "\t\t\t請輸入對應數字選擇功能"
echo -e "\t\t\t 1. 每學科的前3名"
echo -e "\t\t\t 2. 個人總成績前5名"
echo -e "\t\t\t 3. 返回上一級"
echo -e "\t\t\t 4. 退出系統"
echo -e "\t\t\t**********************"
echo "請選擇:"
read choice
case $choice in
1)
echo -e "\t\t\t每學科的前3名"
such_course_sort_score
;;
2)
echo -e "\t\t\t個人總成績前5名"
sort_score_sum_five
;;
3)
Menu3
;;
4)
exit 0
;;
*)
score_count
;;
esac
echo "--------------------------"
echo "按任意鍵繼續……"
read c
score_count
}
#1.添加成績
input_score_info(){ #成績信息輸入
read -p "stu_num:" stu_num
while ! grep -w "$stu_num" "$students_db" > /dev/null ;do
echo "此學號沒有錄入在系統中,請重新輸入正確的學號!"
read -p "stu_num:" stu_num
done
read -p "stu_name:" stu_name
while ! grep -w "$stu_name" "$students_db" > /dev/null ;do
echo "名字和學號不對應,請重新輸入"
read -p "stu_name:" stu_name
done
read -p "course:" course
read -p "score:" score
read -p "score_des:" score_des
while grep -w $stu_num $scores_db|grep -w $course > /dev/null ;do #學號和科目確定成績的唯一性
echo "該學生的此科目成績已經錄入,請再次輸入: "
input_score_info
done
}
add_score(){ #添加成績
input_score_info
echo -e "$stu_num,\t$stu_name,\t$course,\t$score,\t$score_des,\r" >> $scores_db
echo "成功添加學生成績信息,添加的信息為: "
echo -e "學號\t姓名\t科目\t成績\t說明"
grep -w $stu_num $scores_db|grep -w $course #學號和科目確定成績的唯一性
}
#2.刪除成績
del_score(){
read -p "請輸入學生的學號" num
read -p "請輸入該學生要刪除的科目" score
echo "該條成績信息為:"
grep "^$num," $scores_db|grep "$score,"
read -p "確定刪除嗎?[yes|no]" choice
case "$choice" in
yes)
sed -i -e "/^$num,.*$score,/d" $scores_db
if [ "$?" -eq 0 ];then
echo "刪除成功"
else
echo "不存在該條記錄或者刪除失敗"
fi
;;
no)
:
;;
*)
echo "請輸入[yes|no]"
del_score
;;
esac
}
#3.成績列表
display_scores(){
echo ""
echo -e "學號\t姓名\t科目\t 成績 說明"
cat $scores_db|column -t
}
#4.查找成績
search_score_num(){
read -p "輸入要查看的學生的學號" num
read -p "輸入要查看的科目" score
echo -e "學號\t姓名\t科目\t成績\t說明"
grep "^$num," $scores_db|grep "$score,"
}
#5.修改成績
modify_score() {
echo "--------------------------"
read -p "輸入學號(不能有空格):" num
while ! grep "^$num," "$scores_db" > /dev/null;do
echo "不存在該編號的學生,請重新輸入:"
read num
done
#防止輸入空格
while [ "$num" = '' ];do #num=$(printf "$num"|sed s/[[:space:]]//g)
echo "輸入有誤,請重新:"
read -p "輸入學號(不能有空格):" num
done
read -p "輸入科目:" course
#防止輸入空格
while [ "$course" = '' ];do
echo "輸入有誤,請重新:"
read -p "輸入科目:" course
done
while ! grep "^$num,.*$course," "$scores_db" > /dev/null;do
echo "成績表裡不存在該編號的學生,請重新輸入:"
read course
#防止輸入空格
while [ "$course" = '' ];do
echo "輸入有誤,請重新:"
read -p "輸入科目:" course
done
done
echo "--------------------------"
echo -e "學號\t姓名\t科目\t成績\t說明"
grep "^$num," "$scores_db"|grep "$course," #學號和科目確定成績的唯一性
echo "--------------------------"
read -p "要修改的欄位信息:" str1
#防止輸入空格
while [ "$str1" = '' ];do
echo "輸入有誤,請重新:"
read -p "要修改的欄位信息:" str1
done
while [ "$str1" = "$num" ];do
echo "請輸入其他欄位(成績表裡不能瞎改學號,如果非要改,請修改學生表:)"
read str1
#防止輸入空格
while [ "$str1" = '' ];do
echo "輸入有誤,請重新:"
read -p "要修改的欄位信息:" str1
done
done
scores_db_studentName=$(grep "^$num," "$scores_db"|sed -n "1p"|cut -d "," -f 2|cut -f 2) #從成績表裡獲取學生姓名
while [ "$str1" = "$scores_db_studentName" ];do
echo "請輸入其他欄位(成績表裡不能瞎改姓名,如果非要改,請修改學生表:)"
read str1
#防止輸入空格
while [ "$str1" = '' ];do
echo "輸入有誤,請重新:"
read -p "要修改的欄位信息:" str1
done
done
while ! grep "^$num,.*$course," "$scores_db"|grep "$str1,"> /dev/null;do
echo "不存在該欄位,請重新輸入:"
read str1
#防止輸入空格
while [ "$str1" = '' ];do
echo "輸入有誤,請重新:"
read -p "要修改的欄位信息:" str1
done
done
read -p "修改後的欄位信息:" str2
#防止輸入空格
while [ "$str2" = '' ];do
echo "輸入有誤,請重新:"
read -p "修改後的欄位信息:" str2
done
read -p "確定刪除嗎?[yes|no]" choice
case "$choice" in
yes)
sed -i "/^$num,.*$course,/ s/$str1/$str2/" $scores_db
if [ "$?" -eq 0 ];then
echo "------------修改成功--------------"
echo "修改了的信息"
echo -e "學號\t姓名\t科目\t成績\t說明"
grep "^$num,.*$course," "$scores_db" #學號和科目確定成績的唯一性
echo "--------------------------"
else
echo "修改失敗"
fi
;;
no)
:
;;
*)
echo "請輸入[yes|no]"
modify_score
;;
esac
}
#6.個人總成績前5名排序
sort_score_sum_five(){
echo "-----------總成績前1到5名---------------"
printf "學號\t\t總成績\t排名\t\n";
awk -F"," '{ary[$1]+=$4} END{for(key in ary) print key ",\t" ary[key]",\t"}' $scores_db|sort -r -n -t',' -k 2| awk -F"," '{print $1 ",\t" $2",\t" NR ",\t"}'|sed -n '1,5p'
echo "--------------------------"
}
#7.每個學科的前3名
such_course_sort_score(){
awk 'BEGIN{FS=","}{ary[$3]=$3} END{for(key in ary) print ary[key]}' $scores_db > temp
size=$(cat temp|wc -l)
top=1
while [ "$top" -le "$size" ];do
var=$(sed -n "$top p" temp)
grep "$var," $scores_db |sort -r -n -t "," -k 4 |sed -n '1,3p'|awk 'BEGIN{FS="," ; printf "科目\t學號\t姓名\t排名\t\n"}{printf $3 "\t" $1 "\t" $2 "\t" NR "\t\n"}'|column -t
echo ""
top=$(($top+1))
done
rm -rf temp
}
#程式入口
haha
exit 0