From 9becc5396af2ba2509c7366e0586c8ee00aa9da8 Mon Sep 17 00:00:00 2001 From: miguel Date: Wed, 28 Dec 2022 19:16:58 +0000 Subject: [PATCH] feat(easy-conditions): add subject, test and solution for the exercise --- sh/tests/easy-conditions_test.sh | 15 ++++++ sh/tests/solutions/easy-conditions.sh | 10 ++++ subjects/devops/easy-conditions/README.md | 65 +++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100755 sh/tests/easy-conditions_test.sh create mode 100755 sh/tests/solutions/easy-conditions.sh create mode 100644 subjects/devops/easy-conditions/README.md diff --git a/sh/tests/easy-conditions_test.sh b/sh/tests/easy-conditions_test.sh new file mode 100755 index 000000000..c06037e22 --- /dev/null +++ b/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 diff --git a/sh/tests/solutions/easy-conditions.sh b/sh/tests/solutions/easy-conditions.sh new file mode 100755 index 000000000..0b1df7359 --- /dev/null +++ b/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 diff --git a/subjects/devops/easy-conditions/README.md b/subjects/devops/easy-conditions/README.md new file mode 100644 index 000000000..dbefae8bd --- /dev/null +++ b/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!