1
0
Fork 0
mirror of https://codeberg.org/Mo8it/How_To_Linux.git synced 2024-11-24 14:11:35 +00:00

Add if block

This commit is contained in:
Mo 2022-09-26 20:56:41 +02:00
parent 08c686d8cf
commit afebcbabc1

View file

@ -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. 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 ```bash
#!/usr/bin/bash #!/usr/bin/bash
@ -73,7 +73,7 @@ This means that we can now write this script:
print("Hello world!") 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 ```console
$ chmod u+x hello_world.py $ 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. 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.
<!-- TODO: if --> ## Conditions
<!-- TODO: test --> ### `if` block
<!-- TODO: if [ ! -f ] --> 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
<!-- TODO: else --> <!-- TODO: else -->
<!-- TODO: else if --> <!-- TODO: else if -->
<!-- TODO: test -->
<!-- TODO: if [ ! -f ] -->
<!-- TODO: case --> <!-- TODO: case -->
<!-- TODO: for --> <!-- TODO: for -->