Skip to content

Instantly share code, notes, and snippets.

@celestialphineas
Created July 15, 2017 06:57
Show Gist options
  • Save celestialphineas/6123923979e729b523214be102288b77 to your computer and use it in GitHub Desktop.
Save celestialphineas/6123923979e729b523214be102288b77 to your computer and use it in GitHub Desktop.
Homework management in shell :)
#!/bin/bash
# A simple homework management system in bash
#============================================
# By: Celestial Phineas @ ZJU (Yehang YIN)
# July 14, 2017
# No database is used in this project.
# It is based on pure ASCII text files.
# Serves as a homework and demo.
# Clear the screen
#=================
clear
# Global variable declarations
#=============================
# The type of the user
# 0 for admin
# 1 for teachers
# 2 for students
declare user_type
# Read-only user type enumeration
declare -r admin_user=0
declare -r teacher_user=1
declare -r student_user=2
# Global user ID
declare user_id
# "this" is the variable that calls the program itself
this=$0
# Intialize files
# ===============
# If the config admin list does not exist, create new
if test ! -f adminpw; then
touch adminpw
printf admin2017 >adminpw
echo "Default password for admin is 'admin2017'"
fi
# Or if the teacher use list does not exist, create new
if test ! -f teacherlst; then
touch teacherlst
fi
# Or if the student user list does not exist, create new
if test ! -f studentlst; then
touch studentlst
fi
# Or if the course list does not exist, create new
if test ! -f courselst; then
touch courselst
fi
# Or if the homework folder does not exist create new
if test ! -d homework; then
mkdir homework
fi
# The login logic
#================
echo Login Homework Management System
# Read the user ID input
printf "User ID> "
read input_id
# Restart if emtpy is met
if test ${#input_id} -eq 0; then
exec $0
fi
# Test if the user ID input is admin
if test $input_id == admin
then
# Get the password from keyboard input
printf "Password> "
read input_password
# Test if the input password is correct
if test $input_password != $(cat adminpw | head -n 1); then
printf "Wrong admin password."
# Pause
read
# Restart the program
exec $0
fi
# Do the following if the password is correct
user_type=$admin_user
user_id=$input_id
# break
# Login success
else # that the input id is not admin
# Find the teacher and student users in the user files respectively
declare -a found_teacher found_student
found_teacher=($(cat teacherlst | grep "^$input_id"))
found_student=($(cat studentlst | grep "^$input_id"))
# Test if there is at least one user is found
if test $(expr ${#found_teacher[@]} + ${#found_student[@]}) -eq 0; then
# If not, do the following
printf "Found no available user \"$input_id\""
# Pause
read
# Restart the login
exec $0
fi
# Now that the user ID does exist
# Read in the password
printf "Password> "
read input_password
found_teacher=($(printf "%s\n" ${found_teacher[@]} | grep "^[0-9]*;$input_password;"))
found_student=($(printf "%s\n" ${found_student[@]} | grep "^[0-9]*;$input_password;"))
# If the password is from a teacher
if test ${#found_teacher[@]} -ne 0
then
user_type=$teacher_user
user_id=$input_id
# Else if the password is from a student
elif test ${#found_student[@]} -ne 0
then
user_type=$student_user
user_id=$input_id
# Else the password is wrong
else
printf "Wrong password."
# Pause
read
# Restart the program
exec $0
fi
fi
# Teacher operations for admin
#=============================
add_teacher()
{
# Loop for adding teachers one by one
while true; do
clear
echo "Add new teacher"
# Get the input
printf "ID> ";read temp_id
# If this is an empty input
if test ${#temp_id} -eq 0; then
continue
fi
# If the input ID already exists
found_teacher=($(cat teacherlst | grep "^$temp_id;"))
if test ${#found_teacher[@]} -ne 0; then
echo "Conflict ID!"
# Pause
read
continue
fi
printf "Initial password> ";read temp_password
# If empty password met
if test ${#temp_password} -eq 0; then
continue
fi
printf "First name> ";read temp_first_name
printf "Last name> ";read temp_last_name
# Let the user confirm the input
while true; do
echo "Sure to add $temp_first_name $temp_last_name to the teacher list? (y/n)"
read choice
case $choice in
y)
echo "$temp_id;$temp_password;$temp_first_name;$temp_last_name" >>teacherlst
break
;;
n) echo "Discard changes."
break
;;
*)
esac
done
# Ask the user if continue adding
echo "Continue adding? (y/n)"
read choice
case $choice in
# If choose to continue, start a new loop
y) continue
;;
# Else exit the adding session
*) break
esac
done
}
delete_teacher()
{
while true; do
clear
# Get the ID of the teacher to delete
printf "Enter the teacher's ID to delete> ";read temp_id
# Find the teacher
found_teacher=($(cat teacherlst | grep -ns "^$temp_id;"))
# If the teacher is not found, try again
if test ${#found_teacher[@]} -eq 0; then
echo "Cannot find ID $temp_id !"
# Pause
read
continue
fi
# Get the line number
# Let a be the found line
local a=${found_teacher[0]}
# Replace the spliter ":" with a space
a=(${a/:/ })
# Let the user confirm
echo "Sure to delete? (y/n)"
printf "%s\t%s\t%s\t%s\n" ${a[1]//;/ }
read choice
case $choice in
y)
;;
*) continue
esac
# Then the line number is the first element of the array
local line_number=${a[0]}
# Delete the line_number-th line of teacherlst
# Create a temp file
touch ".temp"
# Get the head of teacherlst
head -n $((line_number - 1)) teacherlst >".temp"
# Then get the tail
tail -n +$((line_number + 1)) teacherlst >>".temp"
# Write back
cat ".temp" >teacherlst
rm ".temp"
# Delete all files of the teacher's course
local b=(${a[1]//;/ })
rm -f ${b[0]}_*
# Ask the user if to continue deletion
echo "Continue to delete? (y/n)"
read choice
case $choice in
# If choose to continue, start a new loop
y) continue
;;
# Else exit the removal session
*) break
esac
done
}
edit_teacher()
{
while true; do
clear
# Get the ID of the teacher to delete
echo "Enter the teacher's ID to edit> ";read temp_id
# Find the teacher
found_teacher=($(cat teacherlst | grep -ns "^$temp_id;"))
# If the teacher is not found, try again
if test ${#found_teacher[@]} -eq 0; then
echo "Cannot find ID $temp_id !"
# Pause
read
break
fi
# Get the line number
# Let a be the found line
local a=${found_teacher[0]}
# Replace the splitter ":" with a space
a=(${a/:/ })
printf "Old record: %s\t%s\t%s\t%s\n" ${a[1]//;/ }
# Then the line number is the first element of the array
local line_number=${a[0]}
# Delete the line_number-th line of teacherlst
# Create a temp file
touch ".temp"
# Get the head of teacherlst
head -n $((line_number - 1)) teacherlst >".temp"
# Here to create new
printf "New password> ";read temp_password
# If empty password met
if test ${#temp_password} -eq 0; then
echo "Empty input. Discard changes."
# Pause
read
continue
fi
printf "New first name> ";read temp_first_name
printf "New last name> ";read temp_last_name
# Let the user confirm the input
while true; do
echo "Sure to edit $temp_first_name $temp_last_name in the teacher list? (y/n)"
read choice
case $choice in
y)
echo "$temp_id;$temp_password;$temp_first_name;$temp_last_name" >>".temp"
# Then get the tail
tail -n +$((line_number + 1)) teacherlst >>".temp"
# Write back
cat ".temp" >teacherlst
rm ".temp"
break
;;
n) echo "Discard changes."
rm ".temp"
break
;;
*)
esac
done
# Ask the user if to continue deletion
echo "Continue to edit? (y/n)"
read choice
case $choice in
# If choose to continue, start a new loop
y) continue
;;
# Else exit the removal session
*) break
esac
done
}
list_teacher()
{
clear
local a=($(cat teacherlst))
printf "%10s %15s %20s %20s\n" "ID" "Password" "First name" "Last name"
echo "---------- ---------------\
-------------------- --------------------"
printf "%10s %15s %20s %20s\n" ${a[@]//;/ }
# Pause
read
}
# Editing the teachers sub-menu
editing_teachers()
{
# User choice
while true; do
clear
echo "a) Add new teachers"
echo "e) Edit teachers"
echo "d) Delete teachers"
echo "l) List teachers"
echo "b) Back"
read choice
case $choice in
# Must be aware that the break statement is to break the while
# Not to break the case!
a) add_teacher;continue
;;
e) edit_teacher;continue
;;
d) delete_teacher;continue
;;
l) list_teacher;continue
;;
b) break
;;
*) continue
esac
done
}
# Course operations for admin
#============================
add_course()
{
# Loop for adding teachers one by one
while true; do
clear
echo "Add new course"
# Get the input
printf "ID> ";read temp_id
# If this is an empty input
if test ${#temp_id} -eq 0; then
continue
fi
# If the input ID already exists
found_course=($(cat courselst | grep "^$temp_id;"))
if test ${#found_course[@]} -ne 0; then
echo "Conflict ID!"
# Pause
read
continue
fi
printf "Teacher ID> ";read temp_teacher
# If empty teacher id is met
if test ${#temp_teacher} -eq 0; then
continue
fi
printf "Course name> ";read temp_course_name
# Let the user confirm the input
while true; do
echo "Sure to add $temp_course_name to the course list? (y/n)"
read choice
case $choice in
y)
echo "$temp_id;$temp_teacher;$temp_course_name" >>courselst
# Create a file for recording the assignments and laboratory
touch "${temp_teacher}_${temp_id}"
break
;;
n) echo "Discard changes."
break
;;
*)
esac
done
# Ask the user if continue adding
echo "Continue adding? (y/n)"
read choice
case $choice in
# If choose to continue, start a new loop
y) continue
;;
# Else exit the adding session
*) break
esac
done
}
delete_course()
{
while true; do
clear
# Get the ID of the course to delete
printf "Enter the course ID to delete> ";read temp_id
# Find the course
found_course=($(cat courselst | grep -ns "^$temp_id;"))
# If the course is not found, try again
if test ${#found_course[@]} -eq 0; then
echo "Cannot find ID $temp_id !"
# Pause
read
break
fi
# Get the line number
# Let a be the found line
local a=${found_course[0]}
# Replace the spliter ":" with a space
a=(${a/:/ })
# Let the user confirm
echo "Sure to delete? (y/n)"
printf "%s\t%s\t%s\n" ${a[1]//;/ }
read choice
case $choice in
y)
;;
*) continue
esac
# Then the line number is the first element of the array
local line_number=${a[0]}
# Delete the line_number-th line of courselst
# Create a temp file
touch ".temp"
# Get the head of teacherlst
head -n $((line_number - 1)) courselst >".temp"
# Then get the tail
tail -n +$((line_number + 1)) courselst >>".temp"
# Write back
cat ".temp" >courselst
rm ".temp"
# Delete all files of the course
b=(${a[1]//;/ })
rm -f *_${b[0]}*
# Ask the user if to continue deletion
echo "Continue to delete? (y/n)"
read choice
case $choice in
# If choose to continue, start a new loop
y) continue
;;
# Else exit the removal session
*) break
esac
done
}
edit_course()
{
while true; do
clear
# Get the ID of the teacher to delete
echo "Enter the course ID to edit> ";read temp_id
# Find the course
found_course=($(cat courselst | grep -ns "^$temp_id;"))
# If the teacher is not found, try again
if test ${#found_course[@]} -eq 0; then
echo "Cannot find ID $temp_id !"
# Pause
read
continue
fi
# Get the line number
# Let a be the found line
local a=${found_course[0]}
# Replace the spliter ":" with a space
a=(${a/:/ })
printf "Old record: %s\t%s\t%s\n" ${a[1]//;/ }
# Then the line number is the first element of the array
local line_number=${a[0]}
# Delete the line_number-th line of teacherlst
# Create a temp file
touch ".temp"
# Get the head of teacherlst
head -n $((line_number - 1)) courselst >".temp"
# Here to create new
printf "New teacher's ID> ";read temp_teacher
# If empty password met
if test ${#temp_teacher} -eq 0; then
echo "Empty input. Discard changes."
# Pause
read
continue
fi
printf "New course name> ";read temp_course_name
# Let the user confirm the input
while true; do
echo "Sure to edit $temp_course_name in the course list? (y/n)"
read choice
case $choice in
y)
echo "$temp_id;$temp_teacher;$temp_course_name" >>".temp"
# And here to rename the related files
# First get the old teacher's ID
local b=(${a[1]//;/ })
# Then rename each file starts with the old teacher's ID
for eachfile in $(ls ${b[1]}_${b[0]}*); do
filename="${temp_teacher}${eachfile#${b[1]}}"
mv -f $eachfile $filename
done
break
;;
n) echo "Discard changes."
break
;;
*)
esac
done
# Then get the tail
tail -n +$((line_number + 1)) courselst >>".temp"
# Write back
cat ".temp" >courselst
rm ".temp"
# Ask the user if to continue deletion
echo "Continue to edit? (y/n)"
read choice
case $choice in
# If choose to continue, start a new loop
y) continue
;;
# Else exit the removal session
*) break
esac
done
}
list_course()
{
clear
local a=($(cat courselst))
printf "%10s %15s %20s\n" "ID" "Teacher ID" "Course name"
echo "---------- --------------- --------------------"
printf "%10s %15s %20s\n" ${a[@]//;/ }
# Pause
read
}
# Editing the courses sub-menu
editing_courses()
{
# User choice
while true; do
clear
echo "a) Add new courses"
echo "e) Edit courses"
echo "d) Delete courses"
echo "l) List courses"
echo "b) Back"
read choice
case $choice in
# Must be aware that the break statement is to break the while
# Not to break the case!
a) add_course;continue
;;
e) edit_course;continue
;;
d) delete_course;continue
;;
l) list_course;continue
;;
b) break
;;
*) continue
esac
done
}
add_student()
{
# Loop for adding students continously
while true; do
clear
echo "Add new student"
# Get the input
printf "ID> "; read temp_id
# If this is an empty input
if test ${#temp_id} -eq 0; then
continue
fi
# If the input ID already exists
found_student=($(cat studentlst | grep "^$temp_id;"))
if test ${#found_student[@]} -ne 0; then
echo "Conflict ID!"
# Pause
read
continue
fi
printf "Initial password> "; read temp_password
# If empty student id is met
if test ${#temp_password} -eq 0; then
continue
fi
printf "First name> "; read temp_first_name
printf "Last name> "; read temp_last_name
# Let the user confirm the input
while true; do
echo "Sure to add $temp_first_name $temp_last_name to the student list? (y/n)"
read choice
case $choice in
y)
echo "$temp_id;$temp_password;$temp_first_name;$temp_last_name" >>studentlst
break
;;
n) echo "Discard changes."
break
;;
*)
esac
done
# Ask the user if continue adding
echo "Continue adding? (y/n)"
read choice
case $choice in
# If choose to continue, start a new loop
y) continue
;;
# Else exit the adding session
*) break
esac
done
}
delete_student()
{
while true; do
clear
# Get the ID of the student to delete
printf "Enter the student's ID to delete> "; read temp_id
# Find the student
found_student=($(cat studentlst | grep -ns "^$temp_id;"))
# If the student is not found, try again
if test ${#found_student[@]} -eq 0; then
echo "Cannot find ID $temp_id !"
# Pause
read
continue
fi
# Get the line number
# Let a be the found line
local a=${found_student[0]}
# Replace the spliter ":" with a space
a=(${a/:/ })
# Let the user confirm
echo "Sure to delete? (y/n)"
printf "%s\t%s\t%s\t%s\n" ${a[1]//;/ }
read choice
case $choice in
y)
;;
*) continue
esac
# Then the line number is the first element of the array
local line_number=${a[0]}
# Delete the line_number-th line of the studentlst
# Create a temp file
touch ".temp"
# Get the head of studentlst
head -n $((line_number - 1)) studentlst >".temp"
# Then get the tail
tail -n +$((line_number + 1)) studentlst >>".temp"
# Write back
cat ".temp" >studentlst
rm ".temp"
# Delete all files of the student's course
local b=(${a[1]//;/ })
rm -f ${b[0]}_*
# Ask the user if to continue deletion
echo "Continue to delete? (y/n)"
read choice
case $choice in
# If choose to continue, start a new loop
y) continue
;;
# Else exit the removal session
*) break
esac
done
}
edit_student()
{
while true; do
clear
# Get the ID of the student to delete
echo "Enter the student's ID to edit> "; read temp_id
# Find the student
found_student=($(cat studentlst | grep -ns "^$temp_id"))
# If the student is not found, try again
if test ${#found_student[@]} -eq 0; then
echo "Cannot find ID $temp_id !"
# Pause
read
break
fi
# Get the line number
# Let a be the found line
local a=${found_student[0]}
# Replace the splitter ":" with a space
a=(${a/:/ })
printf "Old record: %s\t%s\t%s\t%s\n" ${a[1]//;/ }
# Then the line number is the first element of the array
local line_number=${a[0]}
# Delete the line_number-th line of studentlst
# Create a temp file
touch ".temp"
# Get the head of studentlst
head -n $((line_number - 1)) studentlst >".temp"
# Here to create new
printf "New password> "; read temp_password
# If empty password met
if test ${#temp_password} -eq 0; then
echo "Empty input. Discard changes."
# Pause
read
continue
fi
printf "New first name> "; read temp_first_name
printf "New last name> "; read temp_last_name
# Let the user confirm the input
while true; do
echo "Sure to edit $temp_first_name $temp_last_name in the student list? (y/n)"
read choice
case $choice in
y)
echo "$temp_id;$temp_password;$temp_first_name;$temp_last_name" >>".temp"
# Then get the tail
tail -n +$((line_number + 1)) studentlst >>".temp"
# Write back
cat ".temp" >studentlst
rm ".temp"
break
;;
# If the user is not to confirm, discard
n) echo "Discard changes."
rm ".temp"
break
;;
*)
esac
done
# Ask the user if to continue deletion
echo "Continue to edit? (y/n)"
read choice
case $choice in
# If choose to continue, start a new loop
y) continue
;;
# Else exit the removal session
*) break
esac
done
}
register_student()
{
clear
# Read in the course ID from the user
printf "Please insert the course ID> "; read input_course_id
# Test if the input is empty, retry
if test ${#input_course_id} -eq 0; then
return
fi
# Test if the course ID exists for the teacher
local find_result=$(grep "$input_course_id;$user_id" courselst)
if test ${#find_result} -eq 0; then
echo "Course $input_course_id does not exist!"
# Pause
read
return
fi
# This is the filename for the assignment list
local filename=$user_id\_$input_course_id
touch $filename
while true; do
clear
# Get the student ID to register
printf "Enter the student ID to register> "; read temp_id
# Find the student
found_student=($(cat studentlst | grep -ns "^$temp_id"))
# If the student is not found, try again
if test ${#found_student[@]} -eq 0; then
echo "Cannot find ID $temp_id !"
# Pause
read
continue
fi
# Else... if the ID already exists
found_student=($(cat $filename | grep -ns "^$temp_id"))
if test ${#found_student[@]} -ne 0; then
echo "Already registered ID $temp_id !"
# Pause
read
continue
fi
echo $temp_id >>$filename
# Ask the user if continue adding
echo "Continue adding? (y/n)"
read choice
case $choice in
# If choose to continue, start a new loop
y) continue
;;
# Else exit the adding session
*) break
esac
done
}
unregister_student()
{
clear
# Read in the course ID from the user
printf "Please insert the course ID> "; read input_course_id
# Test if the input is empty, retry
if test ${#input_course_id} -eq 0; then
return
fi
# Test if the course ID exists for the teacher
local find_result=$(grep "$input_course_id;$user_id" courselst)
if test ${#find_result} -eq 0; then
echo "Course $input_course_id does not exist!"
# Pause
read
return
fi
# This is the filename for the assignment list
local filename=$user_id\_$input_course_id
touch $filename
while true; do
clear
# Get the student ID to register
printf "Enter the student ID to unregister> "; read temp_id
# Find the student
found_student=($(cat $filename | grep -ns "^$temp_id"))
if test ${#found_student[@]} -eq 0; then
echo "Unregistered ID $temp_id !"
# Pause
read
continue
fi
# Get the line number
# Let a be the found line
local a=${found_student[0]}
# Replace the splitter ":" with a space
a=(${a/:/ })
# Let the user confirm
echo "Sure to delete? (y/n)"
printf "%s\n" ${a[1]}
read choice
case $choice in
y)
;;
*) continue
esac
# Then the line number is the first element of the array
local line_number=${a[0]}
# Delete the line_nuber-th line of the list
# Create a temp file
touch ".temp"
# Get the head of the list
head -n $((line_number - 1)) $filename >".temp"
# Then get the tail
tail -n +$((line_number + 1)) $filename >>".temp"
# Write back
cat ".temp" >$filename
rm ".temp"
# Ask the user if to continue deletion
echo "Continue to delete? (y/n)"
read choice
case $choice in
# If choose to continue, start a new loop
y) continue
;;
*) break
esac
done
}
# Editing the students sub-menu
editing_students()
{
# User choice
while true; do
clear
echo "a) Add new students"
echo "e) Edit students"
echo "d) Delete students"
echo "l) List courses"
echo "r) Register students"
echo "u) Unregister students"
echo "b) Back"
read choice
case $choice in
# Mush be aware that the break statement is to break the while
# Not to break the case!
a) add_student; continue
;;
e) edit_student; continue
;;
d) delete_student; continue
;;
l) list_course; continue
;;
r) register_student; continue
;;
u) unregister_student; continue
;;
b) break
;;
*) continue
esac
done
}
# Assignment operations for teachers
#===================================
add_assignment()
{
clear
# Read in the course ID from the user
printf "Please insert the course ID> "; read input_course_id
# Test if the input is empty, retry
if test ${#input_course_id} -eq 0; then
return
fi
# Test if the course ID exists for the teacher
local find_result=$(grep "$input_course_id;$user_id" courselst)
if test ${#find_result} -eq 0; then
echo "Course $input_course_id does not exist!"
# Pause
read
return
fi
# This is the file name of the assignment list
local filename=$user_id\_$input_course_id\_assignment
touch $filename
while true; do
clear
echo "Add new assignment"
# Get the input
printf "ID> "; read temp_id
# If this is an empty input
if test ${#temp_id} -eq 0; then
continue
fi
# If the input ID already exists
found_ass=($(cat $filename | grep "^$temp_id;"))
if test ${#found_ass[@]} -ne 0; then
echo "Conflict ID!"
# Pause
read
continue
fi
# Let the user input the type of the assignment
# This can be "homework", "lab" or something else
printf "Type> "; read temp_type
# If empty input is met
if test ${#temp_type} -eq 0; then
continue
fi
printf "Title> "; read temp_title
# Let the user confirm the input
while true; do
echo "Sure to add $temp_title to the assignments? (y/n)"
read choice
case $choice in
y)
echo "$temp_id;$temp_type;$temp_title" >>$filename
break
;;
n) echo "Discard changes."
break
;;
*)
esac
done
# Ask the user if continue adding
echo "Continue adding? (y/n)"
read choice
case $choice in
# If choose to continue, start a new loop
y) continue
;;
# Else exit the adding session
*) break
esac
done
}
edit_assignment()
{
clear
# Read in the course ID from the user
printf "Please insert the course ID> "; read input_course_id
# Test if the input is empty, retry
if test ${#input_course_id} -eq 0; then
return
fi
# Test if the course ID exists for the teacher
local find_result=$(grep "$input_course_id;$user_id" courselst)
if test ${#find_result} -eq 0; then
echo "Course $input_course_id does not exist!"
# Pause
read
return
fi
# This is the file name of the assignment list
local filename=$user_id\_$input_course_id\_assignment
touch $filename
while true; do
clear
echo "Enter the assignment ID to edit> "; read temp_id
# Find the assignment
found_ass=($(cat $filename | grep -ns "^$temp_id"))
# If the assignment is not found, try again
if test ${#found_ass[@]} -eq 0; then
echo "Cannot find ID $temp_id !"
# Pause
read
break
fi
# Get the line number
# Let a be the found line
local a=${found_ass[0]}
# Replace the splitter ":" with a space
a=(${a/:/ })
printf "Old record: %s\t%s\t%s\t\n" ${a[1]//;/ }
# Then the line number is the first element of the array
local line_number=${a[0]}
# Delete the line_number-th line of assignment list
# Create a temp file
touch ".temp"
# Get the head of assignment list
head -n $((line_number - 1)) $filename >".temp"
# Here to create new
printf "New type> "; read temp_type
# If empty input met
if test ${#temp_type} -eq 0; then
echo "Empty input. Discard changes."
# Pause
read
fi
printf "New assignment name> "; read temp_name
# Let the user confirm the input
while true; do
echo "Sure to edit $temp_name in the assignment list? (y/n)"
read choice
case $choice in
y)
echo "$temp_id;$temp_type;$temp_name" >>".temp"
# Then get the tail
tail -n +$((line_number + 1)) $filename >>".temp"
# Write back
cat ".temp" >$filename
rm ".temp"
break
;;
# If the user is not to confirm, discard
n) echo "Discard changes."
rm ".temp"
break
;;
*)
esac
done
# Ask the user if to continue deletion
echo "Continue to edit? (y/n)"
read choice
case $choice in
# If choose to continue, start a new loop
y) continue
;;
# Else exit the editing session
*) break
esac
done
}
delete_assignment()
{
clear
# Read in the course ID from the user
printf "Please insert the course ID> "; read input_course_id
# Test if the input is empty, retry
if test ${#input_course_id} -eq 0; then
return
fi
# Test if the course ID exists for the teacher
local find_result=$(grep "$input_course_id;$user_id" courselst)
if test ${#find_result} -eq 0; then
echo "Course $input_course_id does not exist!"
# Pause
read
return
fi
# This is the filename for the assignment list
local filename=$user_id\_$input_course_id\_assignment
touch $filename
while true; do
clear
# Get the ID of the assignment to delete
printf "Enter the assignment ID to delete> "; read temp_id
# Find the assignment
found_ass=($(cat $filename | grep -ns "^$temp_id;"))
# If the assignment is not found, try again
if test ${#found_ass[@]} -eq 0; then
echo "Cannot find ID $temp_id !"
# Pause
read
continue
fi
# Get the line number
# Let a be the found line
local a=${found_ass[0]}
# Replace the splitter ":" with a space
a=(${a/:/ })
# Let the user confirm
echo "Sure to delete? (y/n)"
printf "%s\t%s\t%s\n" ${a[1]//;/ }
read choice
case $choice in
y)
;;
*) continue
esac
# Then the line number is the first element of the array
local line_number=${a[0]}
# Delete the line_number-th line of the list
# Create a temp file
touch ".temp"
# Get the head of the list
head -n $((line_number - 1)) $filename >".temp"
# Then get the tail
tail -n +$((line_number + 1)) $filename >>".temp"
# Write back
cat ".temp" >$filename
rm ".temp"
# Ask the user if to continue deletion
echo "Continue to delete? (y/n)"
read choice
case $choice in
# If choose to continue, start a new loop
y) continue
;;
# Else exit the removal session
*) break
esac
done
}
list_assignment()
{
clear
# Read in the course ID from the user
printf "Please insert the course ID> "; read input_course_id
# Test if the input is empty, retry
if test ${#input_course_id} -eq 0; then
return
fi
# Test if the course ID exists for the teacher
local find_result=$(grep "$input_course_id;$user_id" courselst)
if test ${#find_result} -eq 0; then
echo "Course $input_course_id does not exist!"
# Pause
read
return
fi
# This is the filename for the assignment list
local filename=$user_id\_$input_course_id\_assignment
touch $filename
local a=($(cat $filename))
printf "%10s %15s %20s\n" "ID" "Type" "Name"
echo "---------- --------------- --------------------"
printf "%10s %15s %20s\n" ${a[@]//;/ }
# Pause
read
}
# Editing the assignments sub-menu
editing_assignments()
{
# User choice
while true; do
clear
echo "a) Add new assignments"
echo "e) Edit assignments"
echo "d) Delete assignments"
echo "l) List assignments"
echo "b) Back"
read choice
case $choice in
# Must be aware that the break statement is to break the while
# Not to break the case!
a) add_assignment;continue
;;
e) edit_assignment;continue
;;
d) delete_assignment;continue
;;
l) list_assignment;continue
;;
b) break
;;
*) continue
esac
done
}
# Upload homework session
upload_homework()
{
while true; do
clear
printf "Enter the course ID> "
# Read in the input
read temp_id
# If empty
if test ${#temp_id} -eq 0; then
# Retry
continue
fi
# Check if the ID exists
local found_id=$(grep "^$temp_id;" courselst)
# If not found
if test ${#found_id[@]} -eq 0; then
echo "Course $temp_id not found!"
# Pause
read
# Retry
continue
fi
# Get the input of assignment ID
printf "Enter assignment ID> "
# Read in the input
read temp_ass_id
# If empty
if test ${#temp_ass_id} -eq 0; then
# Retry
continue
fi
# Check if the assignment exists
if test ! -f *\_$temp_id\_assignment; then
# The assignment does not exist
echo "No such assignment!"
# Pause
read
continue
fi
# Check if the assignment exists
local found_ass=$(grep "^$temp_ass_id;" *\_$temp_id\_assignment)
if test ${#found_ass} -eq 0; then
echo "No such assignment!"
# Pause
read
continue
fi
# Get the user input of path
printf "Enter the file path> "; read temp_path
# Check if it is a file
if test ! -f $temp_path; then
echo "File not found!"
# Pause
read
return
fi
# Copy the file to the homework folder
cp -f $temp_path homework/$temp_id\_$user_id\_$temp_ass_id
echo "Upload successs!"
return
done
}
# Check out the homework submission
check_out()
{
while true; do
clear
printf "Enter the course ID> "
# Read in the course ID
read temp_id
# If empty
if test ${#temp_id} -eq 0; then
# Retry
continue
fi
# Or if not found in the course list
local found_id=$(grep "^$temp_id;" courselst)
# If not found
if test ${#found_id[@]} -eq 0; then
echo "Course $temp_id not found!"
# Pause
read
# Retry
continue
fi
# Read in the assignment ID
printf "Enter the assignment ID> "; read ass_id
# If empty
if test ${#ass_id} -eq 0; then
# Retry
continue
fi
# Not found
if test ! -f *\_$temp_id\_assignment; then
echo "Assignment not found!"
# Pause
read
# Retry
return
fi
# Or if not found in the assignment list
local found_ass=$(grep "^$ass_id;" *\_$temp_id\_assignment)
# If not found
if test ${#found_ass[@]} -eq 0; then
echo "Assignment not found!"
# Pause
read
# Retry
return
fi
# Get the list of students
local student_list=($(cat *\_$temp_id))
# Traverse the student list
for student in $student_list; do
# If found the students' homework, show that he or she has submitted
# Else show the opposite
if test -f homework/$temp_id\_$student\_$ass_id
then
printf "$student\tAssignment $ass_id submitted.\n"
else
printf "$student\tAssignment $ass_id not submitted.\n"
fi
done
# Pause
read
return
done
}
#=========================================================
# Main menu of the admin login
admin_login()
{
while true; do
clear
echo "Welcome, admin!"
echo "t) Edit Teachers"
echo "c) Edit Courses"
echo "l) Log Off"
read choice
case $choice in
t) editing_teachers;continue
;;
c) editing_courses;continue
;;
l) exec $this;continue
;;
*) continue
esac
done
}
teacher_login()
{
while true; do
clear
# Get the teacher's name
local a=$(grep ^$user_id teacherlst)
a=(${a//;/ })
a="${a[2]} ${a[3]}"
echo "Welcome, $a"
echo "s) Edit Students"
echo "a) Edit Assignments"
echo "c) Check out Submission"
echo "l) Log Off"
read choice
case $choice in
s) editing_students;continue
;;
a) editing_assignments;continue
;;
c) check_out;continue
;;
l) exec $this;continue
;;
*) continue
esac
done
}
student_login()
{
while true; do
clear
# Get the teacher's name
local a=$(grep ^$user_id studentlst)
a=(${a//;/ })
a="${a[2]} ${a[3]}"
echo "Welcome, $a"
echo "u) Upload your homework"
echo "l) Log Off"
read choice
case $choice in
u) upload_homework;continue
;;
l) exec $this;continue
;;
*) continue
esac
done
}
#=========================================================
# Case statement for deciding what to do on the login type
case $user_type in
# Function call of admin login
0) admin_login
;;
# Function call of teacher login
1) teacher_login
;;
# Function call of student login
2) student_login
;;
# Default re-login
*) exec $0
esac
#EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment