学生信息管理系统

在Linux下Shell编程实现:

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

具体要求:
(1)构造两个类似数据库的文本文件:
   第一个为学院信息文件(students.db),包含字段:
   学院编号(唯一),学院名称
   第二个为学生信息文件(colleges.db),包含字段:
   学号(唯一),学生姓名,所在系编号
   说明:分隔符可以自己选定,建议用","; 编码规则自己定

(2)文件要求:类似数据库文本文件,每个记录占一行,字段之间用","分割字段含义:
        1)学号(主码)  2)姓名   3)所在系
  以下的每个功能分别作为一个函数
       1.向该文件中插入记录
       2.显示该文件中的每条记录的每个字段值
       3.从该文件中删除指定学号的记录
  shell程序代码如下:

#! /bin/sh

insert_s()

{   

file_name="/root/aillo/students.db"

     dialog --title "Student: Insert a record " --inputbox "Please input the student's information in the sort:(id,name,college):" 20 50 2>tmp.txt

     content=$(cat tmp.txt)

     IFS=","

     read sid sname sin<tmp.txt

     unset IFS

     grep -c "$sid" $file_name>menu.txt

   sid=$(cat menu.txt)

   if [ "$sid" != "0" ];then

      dialog --title "ERROR" --msgbox "Record has existed!" 20 30

   else

      echo "$content">>$file_name

         dialog --title "Reply Info" --msgbox "Add successfully!" 10 30

   fi

}

insert_c()

{   

file_name="/root/aillo/colleges.db"

     dialog --title "College: Insert a record " --inputbox "Please input the college's information in the sort(cid,cname):" 20 50 2>tmp.txt

     content=$(cat tmp.txt)

     IFS=","

     read cid cname<tmp.txt

     unset IFS

     grep -c "$cid" $file_name>tmp.txt

   cid=$(cat tmp.txt)

   if [ "$cid" != "0" ];then

      dialog --title "ERROR" --msgbox "Record has existed!" 30 50

   else

      echo "$content">>$file_name

         dialog --title "Reply Info" --msgbox "Add successfully!" 10 30

   fi

}

delete_s()

{    file_name="/root/aillo/students.db"

     dialog --title "Student: Delete a record " --inputbox "Please input the ID of the student you want to delete:" 20 50 2>tmp.txt

     read sid<tmp.txt

     grep -v $sid $file_name>tmp.txt

     mv tmp.txt $file_name

     dialog --title "Reply Info" --msgbox "the record has been delete!" 10 30

}

delete_c()

{    file_name="/root/aillo/colleges.db"

     dialog --title "College: Delete a record " --inputbox "Please input the ID of the college you want to delete:" 20 50 2>tmp.txt

     read cid<tmp.txt

     grep -v $cid $file_name>tmp.txt

     mv tmp.txt $file_name

     dialog --title "Reply Info" --msgbox "the record has been delete!" 10 30

}

display_s()

{    file_name="/root/aillo/students.db"

     cat $file_name | while read line

     do

         echo $line>tmp.txt

         IFS=","

         read sid sname sin<tmp.txt

         echo "sid: $sid">>t.txt

         echo "sname: $sname">>t.txt

         echo "sin: $sin">>t.txt

         echo "---------------------">>t.txt

         IFS=" "

     done

     content=$(cat t.txt)

     dialog --title "All Students' Info" --msgbox "$content" 50 50

     rm t.txt

}

display_c()

{    file_name="/root/aillo/colleges.db"

     cat $file_name | while read line

     do

         echo $line>tmp.txt

         IFS=","

         read cid cname<tmp.txt

         echo "cid: $cid">>t.txt

         echo "cname: $cname">>t.txt

         echo "---------------------">>t.txt

         IFS=" "

     done

     content=$(cat t.txt)

     dialog --title "All colleges' Info" --msgbox "$content" 50 50

     rm t.txt

}

count()

{   

file_name="/root/aillo/students.db"

     dialog --title "Count " --inputbox "Please input the id of college students you want to count:" 20 50 2>tmp.txt

     read p1<tmp.txt

     dialog --title "Reply Info" --msgbox "There are `grep -c $p1 $file_name` students in the $p1 college!" 10 30

}

change_s()

{   

file_name="/root/aillo/students.db"

     dialog --title "Modify Student's Info " --inputbox "Please input the new information you want to modify in the sort(sid,sname,sin):" 20 50 2>tmp.txt

     IFS=","

     read p1 p2 p3<tmp.txt

     grep -v $p1 $file_name>tmp.txt

     echo "$p1,$p2,$p3">>tmp.txt

     IFS=" "

     mv tmp.txt $file_name

     dialog --title "Reply Info" --msgbox "The information has been modified!" 10 30

}

change_c()

{   

file_name="/root/aillo/colleges.db"

     dialog --title "Modify College's Info" --inputbox "Please input the new information:" 20 50 2>tmp.txt

     IFS=","

     read p1 p2<tmp.txt

     grep -v $p1 $file_name>tmp.txt

     echo "$p1,$p2">>tmp.txt

     IFS=" "

     mv tmp.txt $file_name

     dialog --title "Reply Info" --msgbox "The information has been modified!" 10 30

}

status=1

dialog --title " Students' Info Manage System" --msgbox "\\nWelccome to use the System!\\n" 10 35

if [ $? != 0 ];then

     sleep 1

     dialog --clear

     exit 0

fi

while [ "$status" = "1" ]

do

     dialog --title "Main Menu" --menu "Choices" 15 20 2 1 "Student" 2 "College" 2>tmp.txt

     MY_CHOICE=$(cat tmp.txt)

     if [ $? != 0 ];then

         sleep 1

         dialog --clear

         exit 0

     fi

     if [ "$MY_CHOICE" = "1" ];then

         dialog --title "Student Info Manage" --menu "Choose Operation" 20 30 7 1 "INSERT" 2 "DELETE" 3 "MODIFY" 4 "COUNT" 5 "DISPLAY" 6 "BACK" 7 "EXIT" 2>tmp.txt

         if [ $? != 0 ];then

              sleep 1

              dialog --clear

         exit 0

         fi

         choice_2=$(cat tmp.txt)

         if [ "$choice_2" = "1" ];then

              insert_s

         elif [ "$choice_2" = "2" ];then

              delete_s

         elif [ "$choice_2" = "3" ];then

              change_s

         elif [ "$choice_2" = "4" ];then

              count

         elif [ "$choice_2" = "5" ];then

              display_s

         elif [ "$choice_2" = "6" ];then

              continue

         else

              break

         fi

     else

         dialog --title "College Info Manage" --menu "Choose Operation" 20 30 7 1 "INSERT" 2 "DELETE" 3 "MODIFY" 4 "COUNT" 5 "DISPLAY" 6 "BACK" 7 "EXIT" 2>tmp.txt

         if [ $? != 0 ];then

              sleep 1

              dialog --clear

         exit 0

         fi

         choice_2=$(cat tmp.txt)

         if [ "$choice_2" = "1" ];then

              insert_c

         elif [ "$choice_2" = "2" ];then

              delete_c

         elif [ "$choice_2" = "3" ];then

              change_c

         elif [ "$choice_2" = "4" ];then

              count

         elif [ "$choice_2" = "5" ];then

              display_c

         elif [ "$choice_2" = "6" ];then

              continue

         else

              break

         fi

     fi

     dialog --yesno "Do you want to continue ?" 10 20

     if [ $? = 0 ];then

      status=1

   else

      status=0

   fi

done

sleep 1

dialog --clear

exit 0

注意:在运行程序之前要先创建students.db和colleges.db这两个文件,不然会出错

运行结果:

(1)欢迎界面和主界面



(2)"选择操作"界面



(3)插入一条记录


(4)显示所有的学生/学院信息



(5)删除一条记录



(6)修改记录



(7)经过删除修改后的信息

(8)统计某个学院的学生数