What Is in if Condition in Shell Scripting?

//

Heather Bennett

When writing shell scripts, you often come across the need to perform certain actions based on specific conditions. This is where the if condition comes into play.

The if condition allows you to execute a block of code only if a certain condition is met. Let’s take a closer look at how the if condition works in shell scripting.

The Basic Syntax

The basic syntax of an if condition in shell scripting is as follows:

if [ condition ]
then
    # code to be executed if condition is true
fi

The keyword [ starts the conditional expression and the keyword ] ends it. The square brackets are essential for evaluating the given condition.

The keyword then marks the beginning of the code block that will be executed if the condition evaluates to true. Finally, the keyword fi, which stands for “end if,” marks the end of the if block.

An Example Scenario

To better understand how the if condition works, let’s consider an example scenario where we want to check if a given number is positive or negative:

#!/bin/bash

number=-5

if [ $number -gt 0 ]
then
    echo "The number is positive."
fi

In this example, we assign a value of -5 to a variable called “number”. The conditional expression within the square brackets checks whether the value of “number” is greater than 0 using the “-gt” operator (which means “greater than”).

If this condition evaluates to true, meaning “number” is indeed greater than 0, the code block within the then and fi keywords will be executed. In this case, the output will be “The number is positive. “

Handling Multiple Conditions

In some cases, you might need to check for multiple conditions using logical operators such as AND (-a) or OR (-o). Here’s an example:

number=10

if [ $number -gt 0 -a $number -lt 100 ]
then
echo “The number is between 0 and 100.”
fi

In this example, we use the “-a” operator to represent logical “AND”. The condition checks if “number” is greater than 0 AND less than 100. If both conditions are true, the code block within the then and fi keywords will be executed.

The Else Clause

The if condition can also include an optional else clause. The syntax for adding an else clause is as follows:

if [ condition ]
then
    # code to be executed if condition is true
else
    # code to be executed if condition is false
fi

The else clause allows you to specify a different set of instructions to be executed when the initial condition evaluates to false. Here’s an example:

if [ $number -gt 0 ]
then
echo “The number is positive.”
else
echo “The number is negative or zero.”
fi

In this example, if the value of “number” is not greater than 0, the else block will be executed, and the output will be “The number is negative or zero.”

Nested If Conditions

Shell scripting also allows you to have nested if conditions. This means you can have an if condition within another if condition. Here’s an example to demonstrate this:

if [ $number -gt 0 ]
then
echo “The number is positive.”

if [ $number -lt 100 ]
then
echo “The number is less than 100.”
fi
fi

In this example, we check whether “number” is positive. If it is, we enter the first if block and print “The number is positive.”

Within this block, we check whether “number” is less than 100 using another if condition. If this condition evaluates to true, the code block within the second then and fi keywords will be executed, and “The number is less than 100” will be printed.

In Conclusion

The if condition in shell scripting allows you to make decisions based on specific conditions. By using conditional expressions and logical operators, you can control the flow of your script and execute different blocks of code accordingly. Understanding how to use if conditions effectively is crucial for writing powerful and flexible shell scripts.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy