Browse Source

fix(easy-conditions): Fixing exercise due to misunderstanding of the objective

pull/1718/head
miguel 2 years ago committed by MSilva95
parent
commit
5f8365c19c
  1. 11
      sh/tests/solutions/easy-conditions.sh
  2. 44
      subjects/devops/easy-conditions/README.md

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

@ -1,10 +1 @@
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
[ "$X" -gt "$Y" ] && echo "true" || echo "false"

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

@ -2,44 +2,33 @@
### Instructions
Create a script `easy-conditions.sh` which will verify the following:
For this exercise it will be given a variable "X" and "Y" and you have to create a script `easy-conditions.sh` that will check if "X" is greater than "Y", if it is print "true" and if it's not print "false".
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
Expected output
```console
$ echo $X $Y
6 14
$ ./easy-conditions.sh
X is greater than 5
Y is less than 20
false
$ echo $X $Y
3 29
29 12
$ ./easy-conditions.sh
X is less than 5
Y is greater than 20
true
$
```
### 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:
The `test` command, is a shell builtin that is used to evaluate expressions in a shell script. It has various options for performing different types of tests, such as checking the type of a file, comparing the values of two variables, or testing the status of a command.
There are two syntaxes for using the test command.
```console
if CONDITION; then
COMMAND1
COMMAND2
...
fi
$ test EXPRESSION
$ [ EXPRESSION ]
```
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:
@ -48,18 +37,5 @@ Here is a summary of the "-gt", "-lt", and "-eq" operators:
- "-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