mirror of https://github.com/01-edu/public.git
3 changed files with 111 additions and 0 deletions
@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env bash |
||||
|
||||
# Unofficial Bash Strict Mode |
||||
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") |
||||
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") |
||||
} |
||||
|
||||
challenge |
@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env bash |
||||
|
||||
largest=0 |
||||
|
||||
for i in {1..10}; do |
||||
|
||||
read -p "Enter a number: " num |
||||
if [ $num -gt $largest ]; then |
||||
largest=$num |
||||
fi |
||||
done |
||||
|
||||
echo "The largest number is: $largest" |
@ -0,0 +1,73 @@
|
||||
## greatest-of-all |
||||
|
||||
### 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. |
||||
|
||||
### Usage |
||||
|
||||
```console |
||||
$./greatest-of-all.sh |
||||
Enter a number: 1 # these numbers will be introduced either the user |
||||
Enter a number: 2 |
||||
Enter a number: 3 |
||||
Enter a number: 4 |
||||
Enter a number: 2 |
||||
Enter a number: 42 |
||||
Enter a number: 1 |
||||
Enter a number: 24 |
||||
Enter a number: 45 |
||||
Enter a number: 2 |
||||
The largest number is 45 |
||||
$ |
||||
``` |
||||
|
||||
### 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: |
||||
|
||||
```console |
||||
if CONDITION; then |
||||
COMMAND1 |
||||
COMMAND2 |
||||
... |
||||
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. |
||||
|
||||
Here is a summary of the "-gt", "-lt", and "-eq" operators: |
||||
|
||||
- "-gt": Greater than. This operator checks if the value on the left is greater than the value on the right. |
||||
- "-lt": Less than. This operator checks if the value on the left is less than the value on the right. |
||||
- "-eq": Equal to. This operator checks if the value on the left is equal to the value on the right. |
||||
|
||||
Here are some examples of how to use the "-gt": |
||||
|
||||
```console |
||||
# Set the variables "x" and "y" |
||||
x=5 |
||||
y=10 |
||||
|
||||
# Check if "x" is greater than "y" |
||||
if [ "$x" -gt "$y" ]; then |
||||
echo "x is greater than y" |
||||
fi |
||||
``` |
||||
|
||||
`read -p` is a command in Bash (the Unix shell) that reads a line of input from the user and stores it in a variable. The -p option allows you to specify a prompt to display to the user before reading the input. |
||||
|
||||
Here is an example of how you might use read -p in a Bash script: |
||||
|
||||
```console |
||||
echo "Enter your name: " |
||||
read -p 'Name: ' name |
||||
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]}". |
||||
|
||||
> 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…
Reference in new issue