cs-3424: finish assignment 1

This commit is contained in:
Price Hiller 2024-06-26 15:38:00 -05:00
parent b8b47c1289
commit ca81dac9ba
Signed by: Price
GPG Key ID: C3FADDE7A8534BEB
18 changed files with 438 additions and 0 deletions

View File

@ -0,0 +1,90 @@
#!/usr/bin/env bash
set -eEuo pipefail
SCRIPT_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
LOG_FILE="${SCRIPT_DIR}/data/queries.log"
DATA_DIR="${SCRIPT_DIR}/data"
declare -A COURSE_DATA
log() {
local out
local title="${1?No title provided to log!}"
shift
local msg="${1?No message provided to log!}"
shift
out="[$(date)] ${title}: ${msg}"
printf "%s\n" "$out" >>"$LOG_FILE"
for body_line in "${@}"; do
printf "%s\n" "$body_line" >>"$LOG_FILE"
done
}
show_options() {
cat <<-__EOS__
Enter one of the following actions or press CTRL-D to exit.
C - create a new course record
R - read an existing course record
U - update an existing course record
D - delete an existing course record
E - update enrolled student count of existing course
T - show total course count
__EOS__
}
err() {
printf "ERROR: %s\n" "${*}" 1>&2
}
check_course_file_access() {
local course_file="${1}"
if [[ ! -f "$course_file" ]]; then
err "course not found"
return 1
elif [[ ! -r "$course_file" ]]; then
local course="${course_file##*/}"
course="${course_file##*".crs"}"
err "course ${course} was found, but unable to read from its file"
return 1
fi
}
main() {
. "${SCRIPT_DIR}/create.bash"
. "${SCRIPT_DIR}/delete.bash"
. "${SCRIPT_DIR}/enroll.bash"
. "${SCRIPT_DIR}/read.bash"
. "${SCRIPT_DIR}/total.bash"
. "${SCRIPT_DIR}/update.bash"
show_options
while read -r option; do
option="${option^^}"
case "$option" in
C)
create_course || true
;;
R)
read_course || true
;;
U)
update_course || true
;;
D)
delete_course || true
;;
E)
change_course_enrollment || true
;;
T)
total_courses || true
;;
*)
err "invalid option"
;;
esac
show_options
done
}
main "${@}"

View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -eEuo pipefail
. "${SCRIPT_DIR}/update.bash"
create_course() {
update_course "y" || return
}

View File

@ -0,0 +1 @@
queries.log

View File

@ -0,0 +1,3 @@
BIO Biology
Human Anatomy and Physiology Laboratory II
MWF 8/26/24 12/14/24 3 59

View File

@ -0,0 +1,3 @@
BIO Biology
Plant Cell Biology
TH 8/26/24 12/15/24 3 51

View File

@ -0,0 +1,3 @@
CS Computer Science
Systems Programming
MWF 8/26/24 12/13/24 4 68

View File

@ -0,0 +1,3 @@
GEO Geological Sciences
Economic Geology Laboratory
TH 8/26/24 12/10/24 1 59

View File

@ -0,0 +1,3 @@
MAT Mathematics
Calculus I
TH 8/26/24 12/11/24 3 71

View File

@ -0,0 +1,3 @@
MAT Mathematics
Data Analysis and Interpretation
TH 8/26/24 12/12/24 3 60

View File

@ -0,0 +1,3 @@
MAT Mathematics
Mathematical Foundations of Cryptography
MWF 8/26/24 12/13/24 3 53

View File

@ -0,0 +1,36 @@
c
CS
Computer Science
3423
Systems Programming
TH
8/20/24
12/13/24
4
60
r
cs 3423
r
cs 3424
r
XXX 1234
u
cs
3421
Systems Laboratory
25
r
cs 3423
d
cs 3424
r
cs 3424
e
cs 3423
68
t

View File

@ -0,0 +1,68 @@
This file was generated by using "grep" to filter for lines containing "echo" or "read -p"
invocations, such that you may you use the exact verbiage in your script.
For example, this was the command line used to make this file (notwithstanding a small
amount of manual cleanup/formatting at the end):
for f in *.bash; do echo -e "\n$f:"; \grep -e echo -e "read -p" "$f"; done > a1_strings.txt
assign1.bash:
echo "Enter one of the following actions or press CTRL-D to exit."
echo "C - create a new course record"
echo "R - read an existing course record"
echo "U - update an existing course record"
echo "D - delete an existing course record"
echo "E - update enrolled student count of existing course"
echo "T - show total course count"
*) echo "ERROR: invalid option";;
create.bash:
read -p "Department code: " dept_code
read -p "Department name: " dept_name
read -p "Course number: " course_num
read -p "Course name: " course_name
read -p "Scheduled days: " course_sched
read -p "Course start: " course_start
read -p "Course end: " course_end
read -p "Credit hours: " course_hours
read -p "Enrolled students: " course_size
echo "ERROR: must provide a department code and course number" 1>&2
echo "ERROR: course already exists"
delete.bash:
read -p "Enter a department code and course number: " dept_code course_num
echo "ERROR: course not found"
echo "$course_num was successfully deleted"
enroll.bash:
read -p "Enter a department code and course number: " dept_code course_num
read -p "Enter an enrollment change amount: " change_amt
echo "ERROR: course not found"
read.bash:
read -p "Enter a department code and course number: " dept_code course_num
echo "ERROR: course not found"
echo "Course department: ${dept_code^^} $dept_name"
echo "Course number: $course_num"
echo "Course name: $course_name"
echo "Scheduled days: $course_sched"
echo "Course start: $course_start"
echo "Course end: $course_end"
echo "Credit hours: $course_hours"
echo "Enrolled Students: $course_size"
total.bash:
echo "Total course records: $count"
update.bash:
read -p "Department code: " dept_code
read -p "Department name: " dept_name
read -p "Course number: " course_num
read -p "Course name: " course_name
read -p "Scheduled days: " course_sched
read -p "Course start: " course_start
read -p "Course end: " course_end
read -p "Credit hours: " course_hours
read -p "Enrolled students: " course_size
echo "ERROR: must provide a department code and course number" 1>&2
echo "ERROR: course not found"

View File

@ -0,0 +1,26 @@
#!/usr/bin/env bash
set -eEuo pipefail
delete_course() {
local dept_code
local course_num
local course_name
read -r -p "Enter a department code and course number: " dept_code course_num
dept_code="${dept_code^^}"
local course_file="${DATA_DIR}/${dept_code^^}${course_num}.crs"
check_course_file_access "$course_file" || return
local linenr=0
while read -r line; do
linenr="$((linenr + 1))"
if ((linenr == 2)); then
course_name="$line"
fi
done <"$course_file"
rm -f "$course_file"
log "DELETED" "${dept_code} ${course_num} ${course_name}"
}

View File

@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -eEuo pipefail
. "${SCRIPT_DIR}/read.bash"
change_course_enrollment() {
local dept_code
local course_num
local change_amt
read -r -p "Enter a department code and course number: " dept_code course_num
dept_code="${dept_code^^}"
if [[ -z "$dept_code" ]] || [[ -z "$course_num" ]]; then
err "must provide a department code and course number"
return
fi
read -r -p "Enter an enrollment change amount: " change_amt
if [[ -z "$change_amt" ]]; then
err "no change amount provided"
return
fi
_read_course "$dept_code" "$course_num" || return 1
COURSE_DATA["dept_code"]="$dept_code"
COURSE_DATA["course_num"]="$course_num"
COURSE_DATA["course_size"]=$((COURSE_DATA["course_size"] + change_amt))
_update_course
log "ENROLLMENT" "${dept_code} ${course_num} ${COURSE_DATA["course_name"]}" "changed by ${change_amt}"
}

View File

@ -0,0 +1,68 @@
#!/usr/bin/env bash
set -eEuo pipefail
_read_course() {
local dept_code="${1?No department code provided!}"
local course_num="${2?No course number provided!}"
dept_code="${dept_code^^}"
local course="${dept_code}${course_num}"
local dept_name
local course_name
local course_sched
local course_start
local course_end
local course_hours
local course_size
local course_file="${DATA_DIR}/${course}.crs"
check_course_file_access "$course_file" || return 1
local linenr=0
while read -r line; do
linenr="$((linenr + 1))"
if ((linenr == 1)); then
read -r dept_code dept_name <<<"$line"
elif ((linenr == 2)); then
read -r course_name <<<"$line"
elif ((linenr == 3)); then
read -r course_sched course_start course_end course_hours course_size <<<"$line"
else
err "unable to read ${course_file}, it has an invalid format!"
return 1
fi
done <"$course_file"
COURSE_DATA["dept_code"]="$dept_code"
COURSE_DATA["dept_name"]="$dept_name"
COURSE_DATA["course_num"]="$course_num"
COURSE_DATA["course_name"]="$course_name"
COURSE_DATA["course_sched"]="$course_sched"
COURSE_DATA["course_start"]="$course_start"
COURSE_DATA["course_end"]="$course_end"
COURSE_DATA["course_hours"]="$course_hours"
COURSE_DATA["course_size"]="$course_size"
}
read_course() {
local dept_code
local course_num
read -r -p "Enter a department code and course number: " dept_code course_num
_read_course "$dept_code" "$course_num" || return 1
printf "%s\n" "${COURSE_DATA[*]}"
cat <<-__EOS__
Course department: ${COURSE_DATA["dept_code"]} ${COURSE_DATA["dept_name"]}
Course number: ${COURSE_DATA["course_num"]}
Course name: ${COURSE_DATA["course_name"]}
Scheduled days: ${COURSE_DATA["course_sched"]}
Course start: ${COURSE_DATA["course_start"]}
Course end: ${COURSE_DATA["course_end"]}
Credit hours: ${COURSE_DATA["course_hours"]}
Enrolled Students: ${COURSE_DATA["course_size"]}
__EOS__
}

View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -eEuo pipefail
total_courses() {
local courses=("${DATA_DIR}/"*.crs)
printf "Total course records: %d\n" "${#courses[@]}"
}

View File

@ -0,0 +1,78 @@
#!/usr/bin/env bash
set -eEuo pipefail
. "${SCRIPT_DIR}/read.bash"
_update_course() {
local dept_code="${COURSE_DATA["dept_code"]^^}"
local dept_name="${COURSE_DATA["dept_name"]}"
local course_num="${COURSE_DATA["course_num"]}"
local course_name="${COURSE_DATA["course_name"]}"
local course_sched="${COURSE_DATA["course_sched"]}"
local course_start="${COURSE_DATA["course_start"]}"
local course_end="${COURSE_DATA["course_end"]}"
local course_hours="${COURSE_DATA["course_hours"]}"
local course_size="${COURSE_DATA["course_size"]}"
cat <<-__EOS__ >"${DATA_DIR}/${dept_code}${course_num}.crs"
${dept_code} ${dept_name}
${course_name}
${course_sched} ${course_start} ${course_end} ${course_hours} ${course_size}
__EOS__
}
update_course() {
local create="${1:-}"
local dept_code
local dept_name
local course_num
local course_name
local course_sched
local course_start
local course_end
local course_hours
local course_size
read -r -p "Department code: " dept_code
read -r -p "Department name: " dept_name
read -r -p "Course number: " course_num
read -r -p "Course name: " course_name
read -r -p "Scheduled days: " course_sched
read -r -p "Course start: " course_start
read -r -p "Course end: " course_end
read -r -p "Credit hours: " course_hours
read -r -p "Enrolled students: " course_size
if [[ -z "$dept_code" ]] || [[ -z "$course_num" ]]; then
err "must provide a department code and course number"
return
fi
local course_file="${DATA_DIR}/${dept_code^^}${course_num}.crs"
if [[ "$create" ]]; then
if [[ -f "$course_file" ]]; then
err "course already exists"
return 1
fi
else
_read_course "${dept_code^^}" "$course_num" || return
fi
COURSE_DATA["dept_code"]="${dept_code:-${COURSE_DATA["dept_code"]}}"
COURSE_DATA["dept_name"]="${dept_name:-${COURSE_DATA["dept_name"]}}"
COURSE_DATA["course_num"]="${course_num:-${COURSE_DATA["course_num"]}}"
COURSE_DATA["course_name"]="${course_name:-${COURSE_DATA["course_name"]}}"
COURSE_DATA["course_sched"]="${course_sched:-${COURSE_DATA["course_sched"]}}"
COURSE_DATA["course_start"]="${course_start:-${COURSE_DATA["course_start"]}}"
COURSE_DATA["course_end"]="${course_end:-${COURSE_DATA["course_end"]}}"
COURSE_DATA["course_hours"]="${course_hours:-${COURSE_DATA["course_hours"]}}"
COURSE_DATA["course_size"]="${course_size:-${COURSE_DATA["course_size"]}}"
_update_course
if [[ "$create" ]]; then
log "CREATED" "${COURSE_DATA["dept_code"]} ${COURSE_DATA["course_num"]} ${COURSE_DATA["course_name"]}"
else
log "UPDATED" "${COURSE_DATA["dept_code"]} ${COURSE_DATA["course_num"]} ${COURSE_DATA["course_name"]}"
fi
}