From the course: Learning Bash Scripting

Conditional statements with the "if" keyword - Bash Tutorial

From the course: Learning Bash Scripting

Conditional statements with the "if" keyword

- [Instructor] When we write scripts, we'll often use control structures, which instead of just running script commands from the top down one after the other, give us the opportunity to control and change how script commands are executed based on conditions we specify. One of these control structures is an if statement. And an if statement runs or executes code based on the truth value of a given expression. in Bash, that takes the form of if, followed by an expression from which we'll get a true or false value followed with a keyword then. That's followed by script lines to run, if the condition evaluates as true. We can end the if statement there with fi, the word if spelled backwards, or we can add an else statement and provide script to run, if the condition evaluates as false. The condition is something that returns a truth value, often the extended test with two brackets, but it can also be the older single bracket or an arithmetic evaluation. It can also be a command, because remember, commands always finish with a zero or non-zero status interpreted as true or false. In fact, when you're experimenting, it can be useful to use the true and false commands in your if statements to guarantee a particular result. Let's take a look at how to use this in a script. First, I'll create a variable called a. It's going to be an integer and I'll give it the value three. Then I'll write the if keyword and provide the test. We'll ask if a is greater than four. Following that, I'll use the keyword then. And I'll provide a command to run, if the value is true. On the following line, I'll use the else keyword, and provide something to run, if the evaluation is false. Then I'll close this with fi, remember that's if backwards. I'll make sure to save this and bring back my terminal. and I'll run my script with ./myscript. On line four of the script, we're testing to see if three is greater than four. Three is in fact not greater than four, so I see the message that I specified for when the condition is false. I'll move back to my script here and change this test to an arithmetic evaluation. I'll run this again and I get the same result. This shows how in Bash there's often multiple ways to get the result you need. We're comparing numbers here so it makes sense to use an arithmetic evaluation. But the extended test notation also provides the ability to compare numbers. You might choose to use one or the other based on readability or to maintain consistency with the rest of an existing script. We can also perform additional condition checks if a condition doesn't match the first one. In our example script, if the variable is greater than four, we return a message that it is, and if not, we say it's not. But using an elif statement here, E-L-I-F, short for else if, we can get in the middle of these two conditions and say, okay, if the variable wasn't greater than four was it greater than two? And then if both of those tests return false, then and only then do we get down to the else condition. I'll save my file and run my script. This gives us the opportunity to operate on more than just a single yes no decision, and instead, build out trees of choices based on a series of conditions. It's not uncommon to see the then keyword in if loops put up on the same line as if or elif separated with a semicolon. Then is a separate keyword but we can put more than one keyword or command on a line using a semicolon between each of them. Functionally, it's the same as having then on its own line, so it's an entirely stylistic choice that some people choose to make for readability reasons. We can also write if statements at the command prompt with each command separated with a semicolon, but that starts to get into an area where it's hard to edit and hard to think about. And that's when you'd want to consider moving your commands into a script. If statements are one of the most important control structures in a Bash script, and understanding them will let you create branch logic in your scripts.

Contents