cs-3424: add initial assignment 1

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

View File

@ -0,0 +1,70 @@
#!/usr/bin/env bash
set -eEuo pipefail
SCRIPT_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
DATA_DIR="${SCRIPT_DIR}/data"
LOG_FILE="${SCRIPT_DIR}/data/queries.log"
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}"
for body_line in "${@}"; do
out="${out}\n${body_line}"
done
printf "%s\n" "$out" >"$LOG_FILE"
}
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
}
main() {
printf "%s\n" "OK"
. ./create.bash
. ./delete.bash
. ./enroll.bash
. ./read.bash
. ./total.bash
. ./update.bash
show_options
while read -r option; do
case "$option" in
C)
create_course "$DATA_DIR" || continue
;;
R)
read_course "$DATA_DIR" || continue
;;
U) ;;
D)
delete_course "$DATA_DIR" || continue
;;
E) ;;
T) ;;
*)
printf "ERROR: invalid option\n"
;;
esac
show_options
done
}
main "${@}"

View File

@ -0,0 +1,45 @@
#!/usr/bin/env bash
set -eEuo pipefail
create_course() {
local data_dir="${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 [[ -f "${course_file}" ]]; then
err "course already exists"
return
fi
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__
log "CREATED" "${dept_code} ${course_num} ${course_name}"
}

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 @@
[Wed Jun 26 01:09:21 PM CDT 2024] DELETED: bio 2060 Human Anatomy and Physiology Laboratory II

View File

@ -0,0 +1,30 @@
#!/usr/bin/env bash
set -eEuo pipefail
delete_course() {
local data_dir="${1?No data directory provided to read courses from!}"
local dept_code
local course_num
local course_name
read -r -p "Enter a department code and course number: " dept_code course_num
local course_file="${data_dir}/${dept_code^^}${course_num}.crs"
if [[ ! -f "$course_file" ]]; then
err "course not found"
elif [[ ! -r "$course_file" ]]; then
err "lack permission to read ${course_file}"
fi
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}"
printf "%s %d was successfully deleted\n" "$dept_code" "$course_num"
}

View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -eEuo pipefail

View File

@ -0,0 +1,79 @@
#!/usr/bin/env bash
set -eEuo pipefail
export COURSE_DATA
declare -A COURSE_DATA
_read_course() {
local data_dir="${1?No data directory provided to read courses from!}"
local dept_code="${2?No department code provided!}"
local course_num="${2?No course number provided!}"
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"
if [[ ! -f "$course_file" ]]; then
err "course not found"
return 1
elif [[ ! -r "$course_file" ]]; then
err "course ${course} was found, but unable to read from its file"
return 1
fi
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_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 data_dir="${1?No data directory provided to read courses from!}"
local dept_code
local course_num
read -r -p "Enter a department code and course number: " dept_code course_num
_read_course "$data_dir" "$dept_code" "$course_num" || return 0
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,3 @@
#!/usr/bin/env bash
set -eEuo pipefail

View File

@ -0,0 +1,49 @@
#!/usr/bin/env bash
set -eEuo pipefail
. ./read.bash
_update_course() {
local data_dir="${1?No data directory provided to read courses from!}"
_read_course "$data_dir" "$dept_code" "$course_num" || return 0
local dept_code="${2:-${COURSE_DATA["dept_code"]}}"
local dept_name="${3:-${COURSE_DATA["dept_name"]}}"
local course_num="${4:-${COURSE_DATA["course_num"]}}"
local course_name="${5:-${COURSE_DATA["course_name"]}}"
local course_sched="${6:-${COURSE_DATA["course_sched"]}}"
local course_start="${7:-${COURSE_DATA["course_start"]}}"
local course_end="${8:-${COURSE_DATA["course_end"]}}"
local course_hours="${9:-${COURSE_DATA["course_hours"]}}"
local course_size="${10:-${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 data_dir="${1?No data directory provided to read courses from!}"
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
}