Creating an If Statement in Linux with Control and Read Commands
=================================================================
In this tutorial, we will learn how to create if statements using control commands in Linux. The syntax for creating an if statement is very straightforward.
**Using If Statements**
---------------------
To use an if statement, you need to start by typing "if" followed by the condition that determines whether the code inside the if block should be executed. Then, you need to put a semicolon after the condition and then type "then". This will indicate where the code that should be executed starts.
For example, let's say we want to check if "tim" is equal to "tim". If this is true, then we can print out "tim is great!". Here's how we can do it:
```bash
control tim && echo tim is great!
```
As you can see, the control command has been used here. This means that the code inside the if statement will only be executed if the condition before it is true. In this case, since "tim" is indeed equal to "tim", the code prints out "tim is great!". However, let's test this further by adding a second call with "bill":
```bash
control tim && echo tim is great!
control bill && echo bill is great!
```
Notice that we get "tim is great!" but "bill is not great" because the if statement only ran once.
**Using Else Statements**
----------------------
To add an else statement, you need to type "else" after the then. This means that if the condition before it is false, then the code inside the else block will be executed.
For example, let's say we want to check if "tim" is not equal to "tim". If this is true, then we can print out "tim is not great!". Here's how we can do it:
```bash
control tim && echo tim is great!
else echo tim is not great!
```
As you can see, the else statement was added. Now, when we call control with "bill", we get both messages because the if statement ran once and the else ran.
**Using Elif Statements**
----------------------
You can also have multiple conditions inside an if statement by using elif (short for "else if"). This means that you can add more conditions after the first one. Here's how it works:
```bash
control tim && echo tim is great!
elif [ $2 == "joe" ]; then
echo joe is great!
```
As you can see, we added a new condition that checks if "$2" (which represents the second parameter) equals to "joe". If this is true, then the code prints out "joe is great!".
**Getting Console Input**
------------------------
To get console input from the user, you can use the read command. The read command takes two parameters: the variable name where the input will be stored and the prompt that appears on the screen when the user types in their response.
For example, let's say we want to get the user's name:
```bash
read -p "Name: " name
echo $name
```
As you can see, we added a prompt with "-p" which means prompt. The variable name is stored in "$name". When we run this command, it will print out whatever the user types in.
To store the input from the previous example to the variable "text", we need to change our script slightly:
```bash
read -p "Name: " text
echo $text
```
This way, we can access the stored value of the user's name later on by using the dollar sign.