From afebcbabc1fce1871834da0d86e0cdb959c13ace Mon Sep 17 00:00:00 2001 From: Mo8it Date: Mon, 26 Sep 2022 20:56:41 +0200 Subject: [PATCH] Add if block --- src/day_2/shell_scripting.md | 84 +++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 6 deletions(-) diff --git a/src/day_2/shell_scripting.md b/src/day_2/shell_scripting.md index 370cfd6..b8478eb 100644 --- a/src/day_2/shell_scripting.md +++ b/src/day_2/shell_scripting.md @@ -4,9 +4,9 @@ Task automation requires multiple instructions that have to run on demand. To co Since `bash` is the default shell on most Linux distributions, we learn bash scripting. -## First script +## First bash script -Lets write our first Bash script (also for some more demonstration of the two output forms from the last chapter): +Lets write our first Bash script: ```bash #!/usr/bin/bash @@ -73,7 +73,7 @@ This means that we can now write this script: print("Hello world!") ``` -Lets save this tiny Python script as `hello_world.py`, make it executable (will be explained later) and then run it: +Lets save this tiny Python script as `hello_world.py`, make it executable with `chmod` (will be explained later) and then run it: ```console $ chmod u+x hello_world.py @@ -204,16 +204,88 @@ In a script, you can ask for user input. To do so, you can use the command `read In our first bash script, we use `read` to take the answer of the user. The input is then saved in the variable `ANSWER`. You can choose a different name for this variable. After the line with `read`, you can use the variable storing the input as a normal variable. - +## Conditions - +### `if` block - +Our first bash script checks if the user input which is stored in the variable `ANSWER` equals the variable `RIGHT_ANSWER` which stores the value `1`. + +To check for a condition in Bash, we use the following syntax: + +```bash +if [ CONDITION ] +then + (...) +fi +``` + +Here, `(...)` stands for the commands that we want to run if the condition is true. + +In our first bash script, we check for equality of two variables with a double equal sign `==`. + +`fi` is not a typo! It is just `if` reversed to indicate the end of the `if` block. Although the syntax is not the best, you have to sadly accept it. Bash does not have the best syntax... + +Speaking about syntax: You have to take spaces seriously with conditions. + +For example, if we define the variable `VAR=1`, the following snippets **do not work**: + +1. No space after `[` + ```bash + if [$VAR == 1 ] + then + echo "VAR has the value 1" + fi + ``` + +1. No space before `]` + ```bash + if [ $VAR == 1] + then + echo "VAR has the value 1" + fi + ``` +1. No space before `==` but a space after `==` + ```bash + if [ $VAR== 1 ] + then + echo "VAR has the value 1" + fi + ``` +1. No space after `==` but a space before `==` + ```bash + if [ $VAR ==1 ] + then + echo "VAR has the value 1" + fi + ``` + +But the following snippets **work**: + +1. Space after `[`, before `]`, before `==` and after `==` + ```bash + if [ $VAR == 1 ] + then + echo "VAR has the value 1" + fi + ``` +1. Space after `[` and before `]`. No space before `==` and after `==` + ```bash + if [ $VAR==1 ] + then + echo "VAR has the value 1" + fi + ``` + +### `else` block + + + +