From a1b76b0f0b84da5e2bfa2015a0ff0eef70bb0196 Mon Sep 17 00:00:00 2001 From: miguel Date: Wed, 11 Jan 2023 15:51:20 +0000 Subject: [PATCH] feat(grades): add subject, test and solution for the exercise --- sh/tests/grades_test.sh | 104 +++++++++++++++++++++++++++++ sh/tests/solutions/grades.sh | 29 +++++++++ subjects/devops/grades/README.md | 108 +++++++++++++++++++++++++++++++ 3 files changed, 241 insertions(+) create mode 100755 sh/tests/grades_test.sh create mode 100755 sh/tests/solutions/grades.sh create mode 100644 subjects/devops/grades/README.md diff --git a/sh/tests/grades_test.sh b/sh/tests/grades_test.sh new file mode 100755 index 00000000..ba435973 --- /dev/null +++ b/sh/tests/grades_test.sh @@ -0,0 +1,104 @@ +#!/usr/bin/env bash + +IFS=' +' +script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd) + +challenge() { + + declare -a equals=("Student1 90" "Student2 70" "Student3 50") + declare -a differents=("Student1 92" "Student2 75" "Student3 55" "Student4 25") + + for e in "${equals[@]}"; do + name=$(echo "$e" | awk '{print $1}') + grade=$(echo "$e" | awk '{print $2}') + submitted=$(echo "$name $grade" | bash -c ""$script_dirS"/student/grades.sh 1") + expected=$(echo "$name $grade" | bash -c ""$script_dirS"/solutions/grades.sh 1") + diff <(echo "$submitted") <(echo "$expected") + done + for d in "${differents[@]}"; do + name=$(echo "$d" | awk '{print $1}') + grade=$(echo "$d" | awk '{print $2}') + submitted=$(echo "$name $grade" | bash -c ""$script_dirS"/student/grades.sh 1") + expected=$(echo "$name $grade" | bash -c ""$script_dirS"/solutions/grades.sh 1") + diff <(echo "$submitted") <(echo "$expected") + done + + # Testing multiple students + submitted=$( + bash -c ""$script_dirS"/student/grades.sh 3" < 100) -eq 1 ] || ! [[ "$grade" =~ ^[0-9]+$ ]]; then + echo "Error: The grade '$grade' is not a valid input. Only numerical grades between 0 and 100 are accepted." + exit 1 + fi +done + +for student in "${students[@]}"; do + name=$(echo $student | awk '{print $1}') + grade=$(echo $student | awk '{print $2}') + if [ "$grade" -ge 90 ]; then + echo "$name: You did an excellent job!" + elif [ "$grade" -ge 70 ]; then + echo "$name: You did a good job!" + elif [ "$grade" -ge 50 ]; then + echo "$name: You need a bit more effort!" + else + echo "$name: You had a poor performance!" + fi +done diff --git a/subjects/devops/grades/README.md b/subjects/devops/grades/README.md new file mode 100644 index 00000000..7ca7bfdf --- /dev/null +++ b/subjects/devops/grades/README.md @@ -0,0 +1,108 @@ +## grades + +### Instructions + +Create a script `grades.sh` which will ask the user for a specific number of students in order to evaluate them. After the number is set, the script will ask you to introduce the "names" and "grades" of the persons you wish to evaluate. + +The grades will have a range between 0 and 100 and will be numeric values only. + +According to each student grade you will have to return the following: + +- If the student grade is anything lower than 50 you will return the string ": You had a poor performance!": + +```console +$ ./grades.sh 1 +Student Name #1: Sara +Student Grade #1: 34 +Sara: You had a poor performance! +``` + +- If the student grade is anything equal or greater than 50 you will return the string ": You need a bit more effort!": + +```console +$ ./grades.sh 1 +Student Name #1: Sara +Student Grade #1: 51 +Sara: You need a bit more effort! +``` + +- If the student grade is anything equal or greater than 70 you will return the string ": You did a good job!": + +```console +$ ./grades.sh 1 +Student Name #1: Sara +Student Grade #1: 75 +Sara: You did a good job! +``` + +- If the student grade is anything equal or greater than 90 you will return the string ": You did an excellent job!": + +```console +$ ./grades.sh 1 +Student Name #1: Sara +Student Grade #1: 90 +Sara: You did an excellent job! +``` + +### Error handling + +All errors will print a specific message on **stderr** (ending with a newline) and returns a specific non-zero value: + +- Wrong number of arguments: `"Error: expect 2 arguments"`, exit with `1`. + +- If the student grade is not a number or is greater than 100: `Error: The grade "grade" is not a valid input. Only numerical grades between 0 and 100 are accepted.`, exit with `1`. + +```console +$ ./grades.sh 3 +./grades.sh 2 +Student Name #1: Sara +Student Grade #1: 101 +Error: The grade '101' is not a valid input. Only numerical grades between 0 and 100 are accepted. +$ +``` + +### Usage + +```console +$ ./grades.sh 4 +Student Name #1: Sara # introduced by the user +Student Grade #1: 34 # introduced by the user +Student Name #2: Norman +Student Grade #2: 56 +Student Name #3: James +Student Grade #3: 78 +Student Name #4: Albert +Student Grade #4: 90 +Sara: You had a poor performance! +Norman: You need a bit more effort! +James: You did a good job! +Albert: You did an excellent job! +``` + +### Hints + +In bash, you can use the `read` command to read input from the user and store it in a variable like so: + +`read var_name` + +You can also use the "-p" option to specify a prompt that will be displayed to the user before reading the input. + +Use loops to go through each student. For example the for loop is very helpful. + +```console +for element in sequence +do + # commands to be executed +done +``` + +You can use an array to store multiple pieces of data. To create an array, you can use the `declare` command with the "-a" option: + +`declare -a name_array` + +To add elements to the array, you can use the += operator: + +`name_array+=("Adding this to the array")` + +> You have to use Man or Google to know more about commands flags, in order to solve this exercise! +> Google and Man will be your friends!