Browse Source

feat(easy-conditions): add subject, test and solution for the exercise

pull/1718/head
miguel 1 year ago committed by MSilva95
parent
commit
9becc5396a
  1. 15
      sh/tests/easy-conditions_test.sh
  2. 10
      sh/tests/solutions/easy-conditions.sh
  3. 65
      subjects/devops/easy-conditions/README.md

15
sh/tests/easy-conditions_test.sh

@ -0,0 +1,15 @@
#!/usr/bin/env bash
# Unofficial Bash Strict Mode
set -euo pipefail
IFS='
'
script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd)
for i in $(seq 1 5); do
export X=$(shuf -i 1-20 -n 1)
export Y=$(shuf -i 1-30 -n 1)
submitted=$(bash "$script_dirS"/student/easy-conditions.sh)
expected=$(bash "$script_dirS"/solutions/easy-conditions.sh)
diff <(echo "$submitted") <(echo "$expected")
done

10
sh/tests/solutions/easy-conditions.sh

@ -0,0 +1,10 @@
if [ "$X" -gt 5 ]; then
echo "X is greater than 5"
else
echo "X is less than 5"
fi
if [ "$Y" -lt 20 ]; then
echo "Y is less than 20"
else
echo "Y is greater than 20"
fi

65
subjects/devops/easy-conditions/README.md

@ -0,0 +1,65 @@
## easy-conditions
### Instructions
Create a script `easy-conditions.sh` which will verify the following:
It will be given a variable "X" and "Y" and You have to check:
- If the variable "X" is grater than 5 or not. If it is greater than 5, the script will print "X is greater than 5" and if it's not greater than 5, the script will print "X is less than 5".
- If the variable "Y" is less than 20 or not. If it is less than 20, the script will print "Y is less than 20" and if it's greater than 20, the script will print "Y is greater than 20".
### Usage
```console
$ echo $X $Y
6 14
$ ./easy-conditions.sh
X is greater than 5
Y is less than 20
$ echo $X $Y
3 29
$ ./easy-conditions.sh
X is less than 5
Y is greater than 20
$
```
### 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
```
> 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