Skip to content

Bash Scripting: Control Statements

If Statements in Bash

One of the most fundamental concepts in any programming language is the if statement. In Bash, if statements allow us to make decisions in our scripts. We can execute different pieces of code based on certain conditions.

Here's a simple example of an if statement in Bash:

#!/bin/bash

# Declare a variable
my_num=200

# If statement
if [ $my_num -eq 200 ]
then
    echo "The condition is true"
fi

In this script, we first declare a variable my_num and assign it the value 200. Then we have an if statement that checks if my_num is equal to 200. If the condition is true, it echoes "The condition is true".

The -eq inside the brackets stands for "equal". There are different checks that we can perform within the brackets. For example, -ne stands for "not equal", -gt stands for "greater than", and -lt stands for "less than".

Else Statements in Bash

In addition to if statements, Bash also provides else statements. An else statement can be combined with an if statement to execute a piece of code when the if condition is not met.

Here's an example of an if-else statement in Bash:

#!/bin/bash

# Declare a variable
my_num=300

# If-else statement
if [ $my_num -eq 200 ]
then
    echo "The condition is true"
else
    echo "The condition is false"
fi

In this script, the if condition checks if my_num is equal to 200. If the condition is true, it echoes "The condition is true". If the condition is not met (i.e., my_num is not equal to 200), the else statement is executed, and it echoes "The condition is false".

Checking the Presence of Files

Bash allows us to check for the presence of files on the file system. This can be done using the -f option in an if statement. The -f option checks if a certain file exists and is a regular file.

Here's an example:

#!/bin/bash

# File to check
file="~/my_file"

# If statement to check if file exists
if [ -f $file ]
then
    echo "The file exists"
else
    echo "The file does not exist"
fi

In this script, we're checking if the file my_file exists in the home directory. If it does, the script echoes "The file exists". If it doesn't, the script echoes "The file does not exist".

Checking the Presence of Commands

In addition to checking for the presence of files, we can also check if a certain command is available on the system. This can be done using the command -v command in an if statement.

Here's an example:

#!/bin/bash

# Command to check
cmd="htop"

# If statement to check if command is available
if command -v $cmd >/dev/null
then
    echo "$cmd is available"
else
    echo "$cmd is not available"
fi

In this script, we're checking if the command htop is available on the system. If it is, the script echoes "htop is available". If it's not, the script echoes "htop is not available".

Conclusion

Bash scripting is a powerful tool for automating tasks on Linux and Unix-like operating systems. By understanding concepts like variables, if statements, else statements, and how to check for the presence of files and commands, you can write scripts that automate complex tasks and make your life easier.

What is the role of the "echo" command in bash scripting?

a. To read input from the user.
b. To display information on the screen.
c. To save information for later use.
d. To delete information from the system.

Answer

b

What does the "read" command do in bash scripting?

a. It deletes information from the system.
b. It saves information for later use.
c. It reads input from the user.
d. It creates new commands.

Answer

c

What is the purpose of the "if" statement in bash scripting?

a. To create a loop.
b. To perform conditional operations.
c. To print information on the screen.
d. To delete information from the system.

Answer

b

What is the role of a "for" loop in bash scripting?

a. To perform operations repeatedly.
b. To read input from the user.
c. To print information on the screen.
d. To delete information from the system.

Answer

a

What is the purpose of the "rm" command in bash scripting?

a. To save information for later use.
b. To print information on the screen.
c. To create new commands.
d. To delete files from the system.

Answer

d


Version 1.0

This is currently an early version of the learning material and it will be updated over time with more detailed information.

A video will be provided with the learning material as well.

Be sure to subscribe to stay up-to-date with the latest updates.

Need help mastering Machine Learning?

Don't just follow along — join me! Get exclusive access to me, your instructor, who can help answer any of your questions. Additionally, get access to a private learning group where you can learn together and support each other on your AI journey.