From bc64c2cf035b06ff74b204c588bc74041f5974c1 Mon Sep 17 00:00:00 2001 From: miguel Date: Thu, 19 Jan 2023 18:14:19 +0000 Subject: [PATCH] fixing readme solution and test --- sh/tests/grades_test.sh | 8 +++--- sh/tests/solutions/grades.sh | 5 ++++ subjects/devops/grades/README.md | 45 ++++++++++++++++++-------------- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/sh/tests/grades_test.sh b/sh/tests/grades_test.sh index ba4359730..c9f5e2547 100755 --- a/sh/tests/grades_test.sh +++ b/sh/tests/grades_test.sh @@ -12,15 +12,15 @@ challenge() { 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") + submitted=$(echo -e "$name $grade" | bash -c ""$script_dirS"/student/grades.sh 1") + expected=$(echo -e "$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") + submitted=$(echo -e "$name $grade" | bash -c ""$script_dirS"/student/grades.sh 1") + expected=$(echo -e "$name $grade" | bash -c ""$script_dirS"/solutions/grades.sh 1") diff <(echo "$submitted") <(echo "$expected") done diff --git a/sh/tests/solutions/grades.sh b/sh/tests/solutions/grades.sh index f5a213322..b1be31801 100755 --- a/sh/tests/solutions/grades.sh +++ b/sh/tests/solutions/grades.sh @@ -3,6 +3,11 @@ num_students=$1 declare -a students +if [ $# -ne 1 ]; then + echo "Error: Expect 1 argument only!" + exit 1 +fi + for ((i = 0; i < num_students; i++)); do read -p "Student Name #$((i + 1)): " name read -p "Student Grade #$((i + 1)): " grade diff --git a/subjects/devops/grades/README.md b/subjects/devops/grades/README.md index 7ca7bfdf3..a1f8db4e5 100644 --- a/subjects/devops/grades/README.md +++ b/subjects/devops/grades/README.md @@ -2,62 +2,67 @@ ### 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. +Create a script `grades.sh` which will ask, as an argument, for the user to give 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!": +- 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: 34 -Sara: You had a poor performance! +Student Grade #1: 90 +Sara: You did an excellent job! ``` -- If the student grade is anything equal or greater than 50 you will return the string ": 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: 51 -Sara: You need a bit more effort! +Student Grade #1: 75 +Sara: You did a good job! ``` -- If the student grade is anything equal or greater than 70 you will return the string ": You did a good job!": +- 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: 75 -Sara: You did a good job! +Student Grade #1: 51 +Sara: You need a bit more effort! ``` -- If the student grade is anything equal or greater than 90 you will return the string ": You did an excellent job!": +- 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: 90 -Sara: You did an excellent job! +Student Grade #1: 34 +Sara: You had a poor performance! ``` ### 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`. +- Wrong number of arguments: `"Error: expect 1 argument only!"`, 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 +$ ./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. +./grades.sh 2 +Student Name #1: Bob +Student Grade #1: ten +Error: The grade 'ten' is not a valid input. Only numerical grades between 0 and 100 are accepted. +$ ./grades.sh 1 2 3 +Error: Expect 1 argument only! $ ``` @@ -85,22 +90,22 @@ In bash, you can use the `read` command to read input from the user and store it `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. +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 +```bash 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: +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: +To add elements to the array, you can use the `+=` operator: `name_array+=("Adding this to the array")`