Linux Mint – Using variables and conditional statements in Bash

How to create virtual machines with VirtualBox

We covered a brief introduction to Bash scripting at the end of Chapter 4, An Introduction to the Terminal. In the previous section, we created a simple script that monitors the temperature of your computer’s CPU in almost real time. While useful, this script, in particular, won’t save you a lot of time, since you can use the sensors command to do the same thing with almost the same amount of time. Bash really shines because of the fact that it has all the features you’d expect from an actual programming language, including if statements and variables. Bash scripting is used by many administrators to simplify and automate tasks. Paired with cron, you can easily create scripts to automatically run, and with enough skill, completely set up a Linux computer from a bare installation of a distribution to a fully functioning system for a specific purpose.

Before we continue, recall two very important requirements for scripting. First, the script must be executable. After creating a script (which is essentially just a text file), make it executable with the following chmod command:


chmod +x nameofscript

Another requirement, as mentioned in Chapter 4, An Introduction to the Terminal, is the hashbang as the very first line of the file. This is shown in the following command:


#!/bin/bash

Then, after the hash bang, you can enter any shell commands you would like to be executed, one per line. In addition to shell commands, your script can contain if statements and variables. In Chapter10, Securing Linux Mint, variables were introduced, but in the context of a script for creating a snapshot. We’ll go through a little refresher here before continuing.

In short, a variable is a string of text meant to represent some sort of value. The value it represents can be a number or even text. For example, execute the following command lines either in a script or inside the terminal itself, one after the other:


hellomessage='Hello World'
mynumber=5

With these command lines, you created two variables. Notice that there was no output; you were simply brought back to the prompt. Don’t let this fool you though; the commands definitely did something.

To see the results, try the following two commands:


echo $hellomessage
echo $mynumber

Note

If you close your terminal window, the values of any variables you created will be instantly forgotten. To make variables reinitialize each time you open a terminal window, recall the previous section on making aliases permanent. You can add variables, and not just aliases, to the .bashrc file.

As you can see, the values you typed were recalled. So, what exactly happened? First, we declared two variables. The first was a string (a line of text). Notice that we didn’t include a space on either side of the equal sign. If we did so, the command to create a variable would have failed. The first variable was created with a string of text in quotes, but the second one didn’t include quotes. This is because the term “hello world” included a space. If we’re going to include a space, we need to enclose the contents of our variable in quotes. As the number 5 didn’t include a space, we left the quotes out.

The next two commands used the echo command followed by our variable names. The echo command simply prints out whatever we tell it to. We could have typed the following command to get the same result as the first echo command:


echo "Hello World"

The echo command can also give us the contents of a variable. Consider the following example:


echo $HOSTNAME

The $HOSTNAME variable is built in; we didn’t declare it. This variable includes the name of our PC. So, when we executed echo $hellomessage, we told echo that we want to view the contents of the $hellomessage variable, which we set to “ Hello World” earlier. Notice that when recalling a variable, we prefixed the variable name with a dollar sign, but when creating the variable, we did not.

Next, let’s take a look at conditional statements, also known as branching. In scripts that are useful, it’s rare that simply one command after another will suffice. We may want to run a command only if a specific condition is true. For this, we would use an if statement. Let’s take a look at the following example:


#!/bin/bash

myvar=5
if [ $myvar = 5 ]
then
  echo "The variable equals 5."
else
  echo "The variable doesn't equal 5."
fi

To run the script, save it in a text file and then make it executable. You can run the script by opening a terminal, navigating to where you saved it, and then type the name of the script prefixed with ./, and it will run.

In the script, we first declared a variable named myvar to equal 5. The next line down, we started an if statement. Notice that there is a space in between the opening and closing brackets; this is required and may seem out of place, as variable declarations cannot have spaces, though the if statement in this example needs the spaces. Inside the brackets, we used the equals sign to compare whether or not the myvar variable is equal to 5.

Next, we typed then, which is required in an if statement. The statement directly after then is executed if the condition of the if statement is true. The line after else is executed only if the previous line did not prove true.

However, the if statement included is just a sample of how the syntax worked, as the script would be completely useless in real life. With further reading on Bash scripting, you can perform tasks such as installing a package if it’s not currently installed, creating users, and much more—pretty much anything you can set your mind to.

Comments are closed.