Shell学生成绩管理系统

来源:互联网 发布:便宜的旅游国家知乎 编辑:程序博客网 时间:2024/06/05 14:21


#!/bin/bash#Output file save the students informationFILE=./zhubinqiang_v1.0#temp fileTMP=./.tmp#This is menumenu () {echo "***************************************************************************"echo -e "\t\t1.Add a student information"echo -e "\t\t2.Query a student information by ID" echo -e "\t\t3.Delete a student information by ID"echo -e "\t\t4.Screen clearing"echo -e "\t\t0.Exit this program"echo "***************************************************************************"echo -n "Select:"}#Add a student infomationadd () {#validate IDwhile [ 1 ];doecho -n "Student ID(001-999):"read id flag=`echo $id|grep -c "^[0-9]\{3\}$"`if [ $flag -eq 0 -o "$id" == "000" ] ; thenecho "ID must 001-999"continue fiflag2=`awk '{print $1}' $FILE | grep -c ^${id}$`if [ $flag2 -ge 1 ] ;thenecho "The ID must be only one!"continuefibreakdone#validate Namewhile [ 1 ];doecho -n "Student name(only a-z,A-Z and within 5 characters):"read nameflag=`echo $name|grep -c "^[a-zA-Z]\{1,5\}$"`if [ $flag -eq 0 ] ; thenecho "name must within 5 characters only a-z,A-Z"else #Name into the capital lettersname=`echo $name | tr '[a-z]' '[A-Z]'`breakfidone#validate Sexecho -n "Student sex(M or F,default is M):"read sexif [ "$sex" = "F" -o "$sex" = "f" ] ; thensex=Felse sex=Mfi#validate Classwhile [ 1 ]; doecho -n "Student class(01-99 default is NULL):"read classif [ "$class" == "" ];thenclass=NULLbreakfi #Class match 00-99flag=`echo $class|grep -c "^[0-9]\{2\}$"`#check ?if [ $flag -eq 0 -o "$class" == "00" ] ; thenecho "class must 01-99"elsebreakfidoneecho -e "$id\t$name\t$sex\t$class" >> $FILE}#Query a student by IDquery () {echo -n "Input the student ID:"read idif [ "$id" == "" ] ; thenecho -e "ID\tName\tSex\tClass"cat $FILEelseecho -e "ID\tName\tSex\tClass"grep $id $FILEif [ $? != 0 ]; thenecho "No student information!"fifi}#Delete a student by IDdelete () {echo -n "Input the student ID:"read idflag=`grep -n "$id" $FILE`if [ "$flag" == "" -o "$id" == "" ]; thenecho "can't find ID=$id student"return fi   #get delete numbernum=`awk '{print $1}' $FILE | grep -n "${id}" |sed 's/:.*//g'`echo -e "ID\tName\tSex\tClass"sed -n "${num}p" $FILEecho -n "Are you sure delete id(Y or N):"read sureif [ "$sure" == "Y" -o "$sure" == "y" ] ; thensed "${num}d" $FILE > $TMPcat $TMP > $FILEecho "Deleted successful"fi}#exit programquit () {echo "Thank you Bye!"exit}#Error infoerror () {echo "error input!"}#screen cleaningcls () {clear}#mainmain () {while [ 1 ] ; domenuread numbercase $number in1) add ;;2) query ;;3) delete ;;4) cls ;;0) quit ;;*) error ;;esacdone}main





原创粉丝点击