##Power by oAthEvil ##E-mail: oathevil@gmail.com ##Blog: http://blog.youkuaiyun.com/oathevil #! /bin/bash DIALOG=/usr/bin/dialog TMP="./tmp/tmp.$" FILE_FACULTY="./file/Faculty.dat" FILE_STUDENT="./file/Student.dat" FILE_SCORE="./file/Score.dat" msg() { $DIALOG --title "Prompt" --msgbox "$1" 15 30 } Insert() { case "$1" in Faculty) #### Get faculty id to be inserted $DIALOG --title "$1" --inputbox "Enter [Faculty Id]:" 6 30/ 2> ${TMP}_INSERT_INS faculty_id=$(cat ${TMP}_INSERT_INS) #### Check If the record of $faculty exists export n=$(cat $FILE_FACULTY | grep "^${faculty_id}," | wc -l | tr -d " ") if [ "x$n" != "x0" ] || [ "x$faculty_id" = "x" ] ; then msg "Invalid faculty id or already exsitance !" return fi #### Get faculty name to be inserted $DIALOG --title "$1" --inputbox "Enter [Faculty Name]:" 6 30/ 2> ${TMP}_INSERT_INS faculty_name=$(cat ${TMP}_INSERT_INS) #### Check if faculty id and faculty name to be inserted are valid if [ "x$faculty_id" = "x" ] || [ "x$faculty_name" = "x" ] ; then if [ "x$faculty_id" = "x" ] ; then msg "Invalid faculty id !" else msg "Invalid faculty name !" fi else echo $faculty_id,$faculty_name >> $FILE_FACULTY msg "Insert record successfully !" fi rm -f ${TMP}_INSERT_INS ;; Student) #### Get Student id to be inserted $DIALOG --title "$1" --inputbox "Enter [Student Id]:" 6 30/ 2> ${TMP}_STUDENT_INS student_id=$(cat ${TMP}_STUDENT_INS) #### Check If the record of $student_id exists export n=$(cat $FILE_STUDENT | grep "^${student_id}," | wc -l | tr -d " ") if [ "x$n" != "x0" ] || [ "x$student_id" = "x" ] ; then msg "Invalid student id or already exsitance !" return fi $DIALOG --title "$1" --inputbox "Enter [Student Name]:" 6 30/ 2> ${TMP}_STUDENT_INS student_name=$(cat ${TMP}_STUDENT_INS) if [ "x$student_name" = "x" ] ; then msg "Invalid student name !" return fi $DIALOG --title "$1" --inputbox "Enter [Faculty Id]:" 6 30/ 2> ${TMP}_STUDENT_INS faculty_id=$(cat ${TMP}_STUDENT_INS) if [ "x$faculty_id" = "x" ] ; then msg "Invalid faculty id !" return fi $DIALOG --title "$1" --inputbox "Enter [Student Status]:" 6 30/ 2> ${TMP}_STUDENT_INS student_status=$(cat ${TMP}_STUDENT_INS) if [ "x$student_status" = "x" ] ; then msg "Invalid student status!" return fi echo $student_id,$student_name,$faculty_id,$student_status>> $FILE_STUDENT msg "Insert record successfully !" rm -f ${TMP}_STUDENT_INS ;; Score) #### Get the score info of student $DIALOG --title "$1" --inputbox "Enter [Student Id]:" 6 30/ 2> ${TMP}_SCORE_INS student_id=$(cat ${TMP}_SCORE_INS) if [ "x$student_id" = "x" ] ; then msg "Invalid student id !" return fi $DIALOG --title "$1" --inputbox "Enter [Student Name]:" 6 30/ 2> ${TMP}_SCORE_INS student_name=$(cat ${TMP}_SCORE_INS) if [ "x$student_name" = "x" ] ; then msg "Invalid student name !" return fi $DIALOG --title "$1" --inputbox "Enter [Subject Name]:" 6 30/ 2> ${TMP}_SCORE_INS subject_name=$(cat ${TMP}_SCORE_INS) if [ "x$subject_name" = "x" ] ; then msg "Invalid subject name !" return fi #### Check If the record of ... exists export n=$(cat $FILE_SCORE | grep "^${student_id}," | grep ",${subject_name}," | wc -l | tr -d " ") if [ "x$n" != "x0" ] || [ "x$student_id" = "x" ] ; then msg "The record has already exists !" return fi $DIALOG --title "$1" --inputbox "Enter [Subject Score]:" 6 30/ 2> ${TMP}_SCORE_INS subject_score=$(cat ${TMP}_SCORE_INS) if [ "x$subject_score" = "x" ] ; then msg "Invalid student score !" return fi $DIALOG --title "$1" --inputbox "Enter [Final/Makeup]:" 6 30/ 2> ${TMP}_SCORE_INS comment=$(cat ${TMP}_SCORE_INS) if [ "x$comment" = "x" ] ; then msg "Invalid comment !" return fi echo $student_id,$student_name,$subject_name,$subject_score,$comment >> $FILE_SCORE msg "Insert record successfully !" rm -f ${TMP}_SCORE_INS ;; esac } Delete() { case "$1" in Faculty) #### Get faculty id of the record to be deleted $DIALOG --title "$1" --inputbox "Enter [Faculty Id] to be deleted:" / 6 30 2> ${TMP}_FACULTY_DEL faculty_id=$(cat ${TMP}_FACULTY_DEL) #### Check If the record of $faculty exists export n=$(cat $FILE_FACULTY | grep "^${faculty_id}," | wc -l | tr -d " ") if [ "x$n" == "x0" ] || [ "x$faculty_id" = "x" ] ; then msg "Can not find info of $faculty_id !" return fi #### Get the info that do not contain the record whose id is $faculty_id cat $FILE_FACULTY | grep -v "^${faculty_id}," > ${TMP}_FACULTY_DEL #### Overwrite the file Faculty.dat mv ${TMP}_FACULTY_DEL $FILE_FACULTY msg "Delete Info of $faculty_id successfully !" Student ;; Student) #### Get student id of the record to be deleted $DIALOG --title "$1" --inputbox "Enter [Student Id] to be deleted:" / 6 30 2> ${TMP}_STUDENT_DEL student_id=$(cat ${TMP}_STUDENT_DEL) #### Check If the record of $faculty exists export n=$(cat $FILE_STUDENT | grep "^${student_id}," | wc -l | tr -d " ") if [ "x$n" == "x0" ] || [ "x$student_id" = "x" ] ; then msg "Can not find info of $faculty_id !" return fi #### Get the info that do not contain the record whose id is $faculty_id cat $FILE_STUDENT | grep -v "^${student_id}," > ${TMP}_STUDENT_DEL #### Overwrite the file Faculty.dat mv ${TMP}_STUDENT_DEL $FILE_STUDENT msg "Delete Info of $student_id successfully !" ;; Score) #### Get student id of the record to be deleted $DIALOG --title "$1" --inputbox "Enter [Student Id] to be deleted:" / 6 30 2> ${TMP}_SCORE_DEL student_id=$(cat ${TMP}_SCORE_DEL) if [ "$student_id" = "" ] ; then msg "Invalid student id !" return fi $DIALOG --title "$1" --inputbox "Enter [Subject Name] to be deleted:" / 6 30 2> ${TMP}_SCORE_DEL subject_name=$(cat ${TMP}_SCORE_DEL) if [ "$subject_name" = "" ] ; then msg "Invalid student name !" return fi tmp_record=$(cat $FILE_SCORE | grep "^${student_id}," | grep ",${subject_name},") if [ "$tmp_record" = "" ] ; then msg "Can not find the $subject_name scroe of $student_id !" return fi #### Get the info that do not contain the record whose id is $faculty_id ####cat $FILE_SCORE | grep -v "^/$student_id,[a-zA-Z]+,/$subject_name" > ${TMP}_SCORE_DEL sed "/$tmp_record/d " $FILE_SCORE > ${TMP}_SCORE_DEL #### Overwrite the file Faculty.dat mv ${TMP}_SCORE_DEL $FILE_SCORE msg "Delete Info of $student_id successfully !" ;; esac } Modify() { case "$1" in Faculty) #### Get faculty id to be modified $DIALOG --title "$1" --inputbox "Enter [Faculty Id] to be modified:" 6 30 2> ${TMP}_FACULTY_MODIFY faculty_id=$(cat ${TMP}_FACULTY_MODIFY) #### Check If the record of $faculty exists export n=$(cat $FILE_FACULTY | grep "^${faculty_id}," | wc -l | tr -d " ") if [ "x$n" == "x0" ] || [ "x$faculty_id" = "x" ] ; then msg "Can not find info of $faculty_id " return fi #### Get the origin faculty name old_faculty_name=$(cat $FILE_FACULTY | grep "^${faculty_id}," | awk -F , '{print $2}') #### Prompt the old faculty name when asking user to input the/ new faculty name $DIALOG --title "$1" --inputbox "Input new [Faculty Name]:" 6 30/ "$old_faculty_name" 2> ${TMP}_FACULTY_MODIFY #### Get the new faculty name new_faculty_name=$(cat ${TMP}_FACULTY_MODIFY) if [ "x$new_faculty_name" = "x" ] ; then msg "Invalid faculty name !" return fi #### Replace the text info from $old_faculty_name to $new_faculty_name sed "s/$old_faculty_name/$new_faculty_name/g" $FILE_FACULTY > ${TMP}_FACULTY_MODIFY #### Overwrite the file Faculty.dat mv ${TMP}_FACULTY_MODIFY $FILE_FACULTY msg "The record of $faculty_id has been changed ." ;; Student) $DIALOG --title "$1" --inputbox "Enter [Student Id] to be modified:" 6 30 2> ${TMP}_STUDENT_MODIFY student_id=$(cat ${TMP}_STUDENT_MODIFY) #### Check If the record of $student_id exists export n=$(cat $FILE_STUDENT | grep "^${student_id}," | wc -l | tr -d " ") msg "$(cat $FILE_STUDENT | grep '^${student_id},')" if [ "x$n" == "x0" ] || [ "x$student_id" = "x" ] ; then msg "Can not find info of $student_id" return fi cat $FILE_STUDENT | grep "^${student_id}" | awk -F , '{print $2" "$3" "$4}' > ${TMP}_STUDENT_MODIFY read old_student_name old_faculty_id old_student_status < ${TMP}_STUDENT_MODIFY $DIALOG --title "$1" --inputbox "Input new [Student Name]:" 6 30/ "$old_student_name" 2> ${TMP}_STUDENT_MODIFY new_student_name=$(cat ${TMP}_STUDENT_MODIFY) if [ "$new_student_name" = "" ] ; then msg "Invalid student name !" return fi $DIALOG --title "$1" --inputbox "Input new [Faculty Id]:" 6 30/ "$old_faculty_id" 2> ${TMP}_STUDENT_MODIFY new_faculty_id=$(cat ${TMP}_STUDENT_MODIFY) if [ "$new_faculty_id" = "" ] ; then msg "Invalid faculty id !" return fi $DIALOG --title "$1" --inputbox "Input new [Student Status]:" 6 30/ "$old_student_status" 2> ${TMP}_STUDENT_MODIFY new_student_status=$(cat ${TMP}_STUDENT_MODIFY) if [ "$new_student_status" = "" ] ; then msg "Invalid student status !" return fi ####sed "s/$old_student_name/$new_student_name/g" $FILE_STUDENT > ${TMP}_STUDENT_MODIFY ####sed "s/$old_faculty_id/$new_faculty_id/g" ${TMP}_STUDENT_MODIFY > ${TMP}_STUDENT_MODIFY1 sed "/^${student_id},/d " $FILE_STUDENT > ${TMP}_STUDENT_MODIFY echo $student_id,$new_student_name,$new_faculty_id,$new_student_status >> ${TMP}_STUDENT_MODIFY mv ${TMP}_STUDENT_MODIFY $FILE_STUDENT msg "The record of $student_id has been changed ." ;; Score) $DIALOG --title "$1" --inputbox "Enter [Student Id] to be modified:" / 6 30 2> ${TMP}_SCORE_MODIFY student_id=$(cat ${TMP}_SCORE_MODIFY) if [ "$student_id" = "" ] ; then msg "Invalid student id !" return fi $DIALOG --title "$1" --inputbox "Enter [Subject Name] to be modified:" / 6 30 2> ${TMP}_SCORE_MODIFY subject_name=$(cat ${TMP}_SCORE_MODIFY) if [ "$subject_name" = "" ] ; then msg "Invalid student name !" return fi tmp_record=$(cat $FILE_SCORE | grep "^${student_id}," | grep ",${subject_name},") if [ "$tmp_record" = "" ] ; then msg "Can not find the $subject_name scroe of $student_id !" return fi cat $FILE_SCORE | grep "^${student_id}," | grep ",${subject_name}," | awk -F , '{print $2" "$3" "$4" "$5}' > ${TMP}_SCORE_MODIFY read old_student_name old_subject_name old_student_score old_comment < ${TMP}_SCORE_MODIFY $DIALOG --title "$1" --inputbox "Input new [Student Name]:" 6 30/ "$old_student_name" 2> ${TMP}_SCORE_MODIFY new_student_name=$(cat ${TMP}_SCORE_MODIFY) if [ "$new_student_name" = "" ] ; then msg "Invalid student name !" return fi $DIALOG --title "$1" --inputbox "Input new [Subject Name]:" 6 30/ "$old_subject_name" 2> ${TMP}_SCORE_MODIFY new_subject_name=$(cat ${TMP}_SCORE_MODIFY) if [ "$new_subject_name" = "" ] ; then msg "Invalid subject name !" return fi $DIALOG --title "$1" --inputbox "Input new [Student Score]:" 6 30/ "$old_student_score" 2> ${TMP}_SCORE_MODIFY new_student_score=$(cat ${TMP}_SCORE_MODIFY) if [ "$new_student_score" = "" ] ; then msg "Invalid student score !" return fi $DIALOG --title "$1" --inputbox "Input new [Final/Makeup]:" 6 30/ "$old_comment" 2> ${TMP}_SCORE_MODIFY new_comment=$(cat ${TMP}_SCORE_MODIFY) if [ "$new_comment" = "" ] ; then msg "Invalid comment !" return fi new_record="$student_id,$new_student_name,$subject_name,$new_student_score,$new_comment" sed "s/$tmp_record/$new_record/g" $FILE_SCORE > ${TMP}_SCORE_MODIFY ####sed "s/$old_faculty_id/$new_faculty_id/g" ${TMP}_SCORE_MODIFY > ${TMP}_SCORE_MODIFY1 ####tmp_record=$(cat $FILE_SCORE | grep "^${student_id}," | grep "$subject_name") ####sed "/$tmp_record/d " $FILE_SCORE > ${TMP}_SCORE_MODIFY ####echo $student_id,$new_student_name,$subject_name,$new_student_score,$new_comment >> ${TMP}_SCORE_MODIFY mv ${TMP}_SCORE_MODIFY $FILE_SCORE msg "The record of $student_id has been changed ." ;; esac } ViewInfo() { case "$1" in Faculty) $DIALOG --title "$1 Info" --exit-label QUIT / --textbox $FILE_FACULTY 15 50 ;; Student) $DIALOG --title "$1 Info" --exit-label QUIT / --textbox $FILE_STUDENT 15 50 ;; Score) $DIALOG --title "$1 Info" --exit-label QUIT / --textbox $FILE_SCORE 15 50 ;; *) ;; esac } Score_Count() { count_select="0" while [ "$count_select" != "" ] do $DIALOG --title "Count Info Management" --radiolist/ "Please select one:" 15 50 3 1 "Sum of every subject score" "on"/ 2 "Top 10 of subject" "off" 3 "Top 20 of sum of score" "off" 2> $TMP count_select=$(cat $TMP) case "$count_select" in 1) student_id="0" cat $FILE_SCORE > ${TMP}_SCORE_COUNT IFS=',' read student_id student_name other_str < ${TMP}_SCORE_COUNT if [ "$student_id" = "" ] ; then msg "Invalid data in head !" continue fi while [ "$student_id" != "" ] do export sum=$(cat ${TMP}_SCORE_COUNT | grep "^${student_id}," | awk -F , '{print $4}' / | awk '{sum+=$1}END{print sum}' | tr -d " ") echo $student_id,$student_name,$sum >> ${TMP}_SCORE_COUNT1 sed "/^${student_id},/d" ${TMP}_SCORE_COUNT > ${TMP}_SCORE_COUNT2 mv ${TMP}_SCORE_COUNT2 ${TMP}_SCORE_COUNT read student_id student_name other_str < ${TMP}_SCORE_COUNT done $DIALOG --title "Sum of subject score Info" --exit-label QUIT / --textbox ${TMP}_SCORE_COUNT1 15 50 rm -f ${TMP}_SCORE_COUNT rm -f ${TMP}_SCORE_COUNT1 rm -f ${TMP}_SCORE_COUNT2 ;; 2) $DIALOG --title "$1" --inputbox "Enter [Subject Name]: " / 6 30 2> ${TMP}_SCORE_COUNT subject_name=$(cat ${TMP}_SCORE_COUNT) if [ "$subject_name" = "" ] ; then msg "Invalid subject name !" continue fi cat $FILE_SCORE | grep ",${subject_name}," | sort -r -n -k4 -t , / | head -10 > ${TMP}_SCORE_COUNT top_ten=$(cat ${TMP}_SCORE_COUNT) if [ "$top_ten" = "" ] ; then msg "Found not data about $subject_name" continue fi $DIALOG --title "Top 10 of $subject_name" --exit-label QUIT / --textbox ${TMP}_SCORE_COUNT 15 50 rm -f ${TMP}_SCORE_COUNT ;; 3) student_id="0" cat $FILE_SCORE > ${TMP}_SCORE_COUNT IFS=',' read student_id student_name other_str < ${TMP}_SCORE_COUNT if [ "$student_id" = "" ] ; then msg "Invalid data in head !" continue fi while [ "$student_id" != "" ] do export sum=$(cat ${TMP}_SCORE_COUNT | grep "^${student_id}," | awk -F , '{print $4}' / | awk '{sum+=$1}END{print sum}' | tr -d " ") echo $student_id,$student_name,$sum >> ${TMP}_SCORE_COUNT1 sed "/^${student_id},/d" ${TMP}_SCORE_COUNT > ${TMP}_SCORE_COUNT2 mv ${TMP}_SCORE_COUNT2 ${TMP}_SCORE_COUNT read student_id student_name other_str < ${TMP}_SCORE_COUNT done cat ${TMP}_SCORE_COUNT1 | sort -r -n -k3 -t , / | head -20 > ${TMP}_SCORE_COUNT top_twenty=$(cat ${TMP}_SCORE_COUNT) if [ "$top_twenty" = "" ] ; then msg "Found not data !" continue fi $DIALOG --title "Top 20 of sum of score" --exit-label QUIT / --textbox ${TMP}_SCORE_COUNT 15 50 rm -f ${TMP}_SCORE_COUNT rm -f ${TMP}_SCORE_COUNT1 rm -f ${TMP}_SCORE_COUNT2 ;; *) break;; esac done rm -f $TMP } Faculty() { faculty_select="0" while [ "$faculty_select" != "" ] do $DIALOG --title "Faculty Info Management" --radiolist/ "Please select one:" 15 50 4 1 "Insert" "on" 2 "Delete" / "off" 3 "Modify" "off" 4 "View Info" "off" 2> $TMP faculty_select=$(cat $TMP) case "$faculty_select" in 1) Insert Faculty ;; 2) Delete Faculty ;; 3) Modify Faculty ;; 4) ViewInfo Faculty ;; *) break;; esac done rm -f $TMP } Student() { student_select="0" while [ "$student_select" != "" ] do $DIALOG --title "Student Info Management" --radiolist/ "Please select one:" 15 50 4 1 "Insert" "on" 2 "Delete" / "off" 3 "Modify" "off" 4 "View Info" "off" 2> $TMP student_select=$(cat $TMP) case "$student_select" in 1) Insert Student ;; 2) Delete Student ;; 3) Modify Student ;; 4) ViewInfo Student ;; *) break ;; esac done rm -f $TMP } Score() { score_select="0" while [ "$score_select" != "" ] do $DIALOG --title "Score Info Management" --radiolist/ "Please select one:" 15 50 5 1 "Insert" "on" 2 "Delete" / "off" 3 "Modify" "off" 4 "View Info" "off" 5 "Count" "off" 2> $TMP score_select=$(cat $TMP) case "$score_select" in 1) Insert Score ;; 2) Delete Score ;; 3) Modify Score ;; 4) ViewInfo Score ;; 5) Score_Count ;; *) break ;; esac done rm -f $TMP } main() { $DIALOG --menu "Please select one:" 10 30 3 1 "Faculty"/ 2 "Student" 3 "Score Of Stu" 2> $TMP select=$(cat $TMP) rm -f $TMP #$DIALOG --title "Error" --msgbox "$select" 6 16 case "$select" in 1) Faculty ;; 2) Student ;; 3) Score ;; *) $DIALOG --title "prompt" --msgbox "Thanks for using / this system !" 7 30 exit 1 ;; esac main } #start the program main