From aca6abf2ef9d5137e82321317e651c5ce839f6c0 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sun, 23 Jun 2024 04:09:55 -0500 Subject: [PATCH] cs-3424: finish lab2 --- .../CS-3424/Quizzes/Quiz-2/Assignment.typ | 158 ++++++++++++++++++ .../Quizzes/Quiz-2/even-lines-rigorous.bash | 68 ++++++++ .../Quizzes/Quiz-2/even-lines-simple.bash | 24 +++ 3 files changed, 250 insertions(+) create mode 100644 Summer-2024/CS-3424/Quizzes/Quiz-2/Assignment.typ create mode 100755 Summer-2024/CS-3424/Quizzes/Quiz-2/even-lines-rigorous.bash create mode 100755 Summer-2024/CS-3424/Quizzes/Quiz-2/even-lines-simple.bash diff --git a/Summer-2024/CS-3424/Quizzes/Quiz-2/Assignment.typ b/Summer-2024/CS-3424/Quizzes/Quiz-2/Assignment.typ new file mode 100644 index 0000000..60afd1b --- /dev/null +++ b/Summer-2024/CS-3424/Quizzes/Quiz-2/Assignment.typ @@ -0,0 +1,158 @@ +#set text(font: "FreeSans") +#set page(margin: 1cm) + +#show link: item => [ + #text(blue)[#item] +] + +#show raw: set text(font: "Fira Code") +#show raw.where(block: true): item => { + set par(leading: .5em) + text(size: 0.9em)[#block( + inset: 4pt, + stroke: luma(40%) + .5pt, + fill: luma(97%), + radius: 3pt, + )[#item]] +} + + +#let comment(com) = { + text(size: 0.85em, style: "italic", fill: rgb(64, 90, 95))[#com] +} + + +#let answer(ans) = { + align(center)[#block( + inset: 4pt, + stroke: blue + .5pt, + fill: rgb(0, 149, 255, 5%), + radius: 3pt, + )[#ans]] +} + +#align(center)[#text(size: 1.3em)[= CS 3424 Quiz - Week 3]] +#align(center)[ + #("Price Hiller", "zfp106", "Quiz 2", "CS 3424").join(" ⋄ ") + #v(-.5em) + #text(size: 0.75em)[#block( + inset: 4pt, + radius: 1pt, + stroke: luma(40%) + .2pt, + width: 40%, + fill: luma(97%), + )[#par(leading: .4em)[If you are interested in viewing the source code of this document, you can do so + by clicking #link("https://git.orion-technologies.io/Price/college/src/branch/Development/Summer-2024/CS-3424/Quizzes/Quiz-2/Assignment.typ", "here.")]]] + #v(-.5em) + #line(length: 100%, stroke: (dash: "densely-dotted")) + #v(-.5em) +] + +1. Create a shell script which prints only the even lines from an input file. Assume the first line is line 1. At the end of the script, if the total number of lines is greater than 10, print "big". Otherwise, print "small". + + #answer[ + #comment[Here is the "simpler" way of doing this, only accepting a single file to be read from as an argument and little to no validation.] + ```bash + #!/usr/bin/env bash + + set -eEou pipefail + + main() { + local input="${1}" + local line_count=0 + while read -r line; do + # To satisfy the `set -u` incantation at the top, we have to assign the variable instead of + # a simple ((line_count++)) + line_count=$((line_count + 1)) + if ((line_count % 2 == 0)); then + printf "%s\n" "$line" + fi + done <"$input" + + if ((line_count > 10)); then + printf "big\n" + else + printf "small\n" + fi + } + + main "${@}" + ``` + + #comment[See the next page for a more comprehensive approach.] + ] +#pagebreak() +#answer[ + + #comment[Below is the more comprehensive way of doing this, including support for STDIN (which you can argue _is_ in fact an "input file").] + ```bash + #!/usr/bin/env bash + + set -eEou pipefail + + die() { + printf "%s\n" "${*}" + exit 1 + } + + usage() { + local script + script="$(basename "${0}")" + cat <<-__EOS__ + ==== USAGE ==== + + Provide a file: + ${script} ./some/path/to/a/file + Pipe from stdin: + echo "hello\nworld!" | ${script} + Redirect from stdin: + ${script} < ./some/path/to/a/file + + ==== USAGE ==== + __EOS__ + } + + handle_lines() { + local input="${1}" + local line_count=0 + while read -r line; do + # To satisfy the `set -u` incantation at the top, we have to assign the variable instead of + # a simple ((line_count++)) + line_count=$((line_count + 1)) + if ((line_count % 2 == 0)); then + printf "%s\n" "$line" + fi + done <"$input" + + if ((line_count > 10)); then + printf "big\n" + else + printf "small\n" + fi + } + + main() { + # Check if we should read from stdin or a given file + if [[ -t 0 ]]; then + # Input should be a path to a file + set +u + if [[ -z "${1}" ]]; then + usage + die "No input received!" + fi + set -u + local fpath="${1}" + + if ! [[ -r "$fpath" ]]; then + die "Unable to read a file at: ${fpath}" + fi + handle_lines "$fpath" + else + # Read from stdin + handle_lines "/dev/stdin" + fi + } + + main "${@}" + ``` +] diff --git a/Summer-2024/CS-3424/Quizzes/Quiz-2/even-lines-rigorous.bash b/Summer-2024/CS-3424/Quizzes/Quiz-2/even-lines-rigorous.bash new file mode 100755 index 0000000..71cb601 --- /dev/null +++ b/Summer-2024/CS-3424/Quizzes/Quiz-2/even-lines-rigorous.bash @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +set -eEou pipefail + +die() { + printf "%s\n" "${*}" + exit 1 +} + +usage() { + local script + script="$(basename "${0}")" + cat <<-__EOS__ + ==== USAGE ==== + + Provide a file: + ${script} ./some/path/to/a/file + Pipe from stdin: + echo "hello\nworld!" | ${script} + Redirect from stdin: + ${script} < ./some/path/to/a/file + + ==== USAGE ==== + __EOS__ +} + +handle_lines() { + local input="${1}" + local line_count=0 + while read -r line; do + # To satisfy the `set -u` incantation at the top, we have to assign the variable instead of + # a simple ((line_count++)) + line_count=$((line_count + 1)) + if ((line_count % 2 == 0)); then + printf "%s\n" "$line" + fi + done <"$input" + + if ((line_count > 10)); then + printf "big\n" + else + printf "small\n" + fi +} + +main() { + # Check if we should read from stdin or a given file + if [[ -t 0 ]]; then + # Input should be a path to a file + set +u + if [[ -z "${1}" ]]; then + usage + die "No input received!" + fi + set -u + local fpath="${1}" + + if ! [[ -r "$fpath" ]]; then + die "Unable to read a file at: ${fpath}" + fi + handle_lines "$fpath" + else + # Read from stdin + handle_lines "/dev/stdin" + fi +} + +main "${@}" diff --git a/Summer-2024/CS-3424/Quizzes/Quiz-2/even-lines-simple.bash b/Summer-2024/CS-3424/Quizzes/Quiz-2/even-lines-simple.bash new file mode 100755 index 0000000..43f26e2 --- /dev/null +++ b/Summer-2024/CS-3424/Quizzes/Quiz-2/even-lines-simple.bash @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +set -eEou pipefail + +main() { + local input="${1}" + local line_count=0 + while read -r line; do + # To satisfy the `set -u` incantation at the top, we have to assign the variable instead of + # a simple ((line_count++)) + line_count=$((line_count + 1)) + if ((line_count % 2 == 0)); then + printf "%s\n" "$line" + fi + done <"$input" + + if ((line_count > 10)); then + printf "big\n" + else + printf "small\n" + fi +} + +main "${@}"