Browse Source

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
pull/1717/head
miguel 2 years ago committed by MSilva95
parent
commit
447de57f08
  1. 18
      sh/tests/greatest-of-all_test.sh
  2. 12
      sh/tests/solutions/greatest-of-all.sh
  3. 18
      subjects/devops/greatest-of-all/README.md

18
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

12
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

18
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!

Loading…
Cancel
Save