From 5f8365c19c94fc642f2d42ee6d97fbc724224a27 Mon Sep 17 00:00:00 2001 From: miguel Date: Thu, 29 Dec 2022 16:43:44 +0000 Subject: [PATCH] fix(easy-conditions): Fixing exercise due to misunderstanding of the objective --- sh/tests/solutions/easy-conditions.sh | 11 +----- subjects/devops/easy-conditions/README.md | 44 ++++++----------------- 2 files changed, 11 insertions(+), 44 deletions(-) diff --git a/sh/tests/solutions/easy-conditions.sh b/sh/tests/solutions/easy-conditions.sh index 0b1df735..61420474 100755 --- a/sh/tests/solutions/easy-conditions.sh +++ b/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" diff --git a/subjects/devops/easy-conditions/README.md b/subjects/devops/easy-conditions/README.md index dbefae8b..f9674e9d 100644 --- a/subjects/devops/easy-conditions/README.md +++ b/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!