From 447de57f08366985eff47074e077bec0556355c6 Mon Sep 17 00:00:00 2001 From: miguel Date: Tue, 10 Jan 2023 11:41:55 +0000 Subject: [PATCH] fix(): correcting typos in readme fixing the test work with a range of numbers and add error checking fixing the solution according to the test --- sh/tests/greatest-of-all_test.sh | 18 ++++++++++++------ sh/tests/solutions/greatest-of-all.sh | 12 ++++++++++-- subjects/devops/greatest-of-all/README.md | 18 ++++++++++++++++-- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/sh/tests/greatest-of-all_test.sh b/sh/tests/greatest-of-all_test.sh index 36a6c1ea8..2e3594081 100755 --- a/sh/tests/greatest-of-all_test.sh +++ b/sh/tests/greatest-of-all_test.sh @@ -1,25 +1,31 @@ #!/usr/bin/env bash # Unofficial Bash Strict Mode -set -euo pipefail +# set -euo pipefail IFS=' ' script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd) challenge() { - submitted=$(echo -e "3\n2\n5\n7\n1\n4\n9\n8\n6\n10" | bash -c ""$script_dirS"/student/greatest-of-all.sh") - expected=$(echo -e "3\n2\n5\n7\n1\n4\n9\n8\n6\n10" | bash -c ""$script_dirS"/solutions/greatest-of-all.sh") + submitted=$(echo -e "0\n3\n2\n5\n7\n1\n4\n9\n8\n6\n10" | bash -c ""$script_dirS"/student/greatest-of-all.sh") + expected=$(echo -e "0\n3\n2\n5\n7\n1\n4\n9\n8\n6\n10" | bash -c ""$script_dirS"/solutions/greatest-of-all.sh") diff <(echo "$submitted") <(echo "$expected") submitted=$(echo -e "26\n85\n21\n94\n68\n60\n99\n31\n10\n98\n" | bash -c ""$script_dirS"/student/greatest-of-all.sh") expected=$(echo -e "26\n85\n21\n94\n68\n60\n99\n31\n10\n98\n" | bash -c ""$script_dirS"/solutions/greatest-of-all.sh") diff <(echo "$submitted") <(echo "$expected") - submitted=$(echo -e "53\n59\n95\n76\n42\n10\n49\n59\n98\n75\n" | bash -c ""$script_dirS"/student/greatest-of-all.sh") - expected=$(echo -e "53\n59\n95\n76\n42\n10\n49\n59\n98\n75\n" | bash -c ""$script_dirS"/solutions/greatest-of-all.sh") - diff <(echo "$submitted") <(echo "$expected") submitted=$(echo -e "152\n485\n569\n611\n871\n551\n984\n895\n285\n989\n" | bash -c ""$script_dirS"/student/greatest-of-all.sh") expected=$(echo -e "152\n485\n569\n611\n871\n551\n984\n895\n285\n989\n" | bash -c ""$script_dirS"/solutions/greatest-of-all.sh") diff <(echo "$submitted") <(echo "$expected") + submitted=$(echo -e "152\n10001\n569" | bash -c ""$script_dirS"/student/greatest-of-all.sh") + expected=$(echo -e "152\n10001\n569" | bash -c ""$script_dirS"/solutions/greatest-of-all.sh") + diff <(echo "$submitted") <(echo "$expected") + submitted=$(echo -e "152\n485\nalpha\n" | bash -c ""$script_dirS"/student/greatest-of-all.sh") + expected=$(echo -e "152\n485\nalpha\n" | bash -c ""$script_dirS"/solutions/greatest-of-all.sh") + diff <(echo "$submitted") <(echo "$expected") + submitted=$(echo -e "152\n485\n-45\n45\n" | bash -c ""$script_dirS"/student/greatest-of-all.sh") + expected=$(echo -e "152\n485\n-45\n45" | bash -c ""$script_dirS"/solutions/greatest-of-all.sh") + diff <(echo "$submitted") <(echo "$expected") } challenge diff --git a/sh/tests/solutions/greatest-of-all.sh b/sh/tests/solutions/greatest-of-all.sh index b9468bf7f..4945979e9 100755 --- a/sh/tests/solutions/greatest-of-all.sh +++ b/sh/tests/solutions/greatest-of-all.sh @@ -5,8 +5,16 @@ largest=0 for i in {1..10}; do read -p "Enter a number: " num - if [ $num -gt $largest ]; then - largest=$num + if ! [[ $num =~ ^[0-9]+$ ]]; then + echo "ERROR: Invalid input only positive numerical characters are allowed" + exit 1 + elif [ $num -gt $largest ]; then + if [ $num -gt 10000 ]; then + echo "ERROR: The number entered is too large" + exit 1 + else + largest=$num + fi fi done diff --git a/subjects/devops/greatest-of-all/README.md b/subjects/devops/greatest-of-all/README.md index e3258e699..8009e68c9 100644 --- a/subjects/devops/greatest-of-all/README.md +++ b/subjects/devops/greatest-of-all/README.md @@ -2,7 +2,7 @@ ### Instructions -Create a script `greatest-of-all.sh` which will ask you to input 10 numbers and then it will check what was the biggest number given. You must ask for the number using the string "Enter a number: " and then use the string "The largest number is: " to print the output like in the example bellow. +Create a script `greatest-of-all.sh` which will ask you to input 10 numbers and then it will check what was the biggest number given. You must ask for the number using the string "Enter a number: " and then use the string "The largest number is: " to print the output like in the example below. ### Usage @@ -19,9 +19,21 @@ Enter a number: 24 Enter a number: 45 Enter a number: 2 The largest number is 45 +$ ./greatest-of-all.sh +Enter a number: -14 +ERROR: Invalid input only positive numerical characters are allowed +./greatest-of-all.sh +Enter a number: alpha +ERROR: Invalid input only positive numerical characters are allowed +./greatest-of-all.sh +Enter a number: 10001 +ERROR: The number entered is too large $ ``` +- Only positive numbers up to "10000" will be tested. +- If the given number is greater than "10000" you must print the error message "ERROR: The number entered is too large" and if its not a number or it is a negative number, print the error "ERROR: Invalid input only positive numerical characters are allowed". When either of these errors occurs, the script will print the error message, exit with an exit code of `1`, and will not continue to execute the next line. + ### Hints The `if` condition in the shell is a control structure that allows you to execute a command or block of commands based on a specified condition. The if condition has the following syntax: @@ -36,7 +48,7 @@ fi In this syntax, CONDITION is a test or expression that returns a boolean value (true or false). If the CONDITION is true, the commands inside the then block are executed. If the CONDITION is false, the commands inside the then block are skipped. -The "-gt", "-lt", and "-eq" operators are used in the shell to perform tests and comparisons on values. These operators are commonly used with the [ command (also known as the test command) to check the value of a variable or expression. +The "-gt", "-lt", and "-eq" operators are used in the shell to perform tests and comparisons on values. These operators are commonly used with the "[" command (also known as the test command) to check the value of a variable or expression. Here is a summary of the "-gt", "-lt", and "-eq" operators: @@ -69,5 +81,7 @@ echo "Hello, $name" This script will display the prompt "Enter your name: " and then wait for the user to enter their name. The name that the user enters will be stored in the name variable, and then the script will greet the user with "Hello, [name]}". +The `exit 1` command is used to indicate that the script has exited with an error. The number "1" is known as an exit code or exit status. + > 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!