How Do You Write an if-Else Condition in Shell Scripting?

//

Larry Thompson

Shell scripting is a powerful tool for automating tasks in a Unix or Linux environment. One of the most fundamental concepts in shell scripting is the if-else condition. In this article, we will explore how to write an if-else condition in shell scripting and understand its syntax and usage.

Syntax:

The syntax for writing an if-else condition in shell scripting is as follows:

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

Let’s break down the syntax and understand each component.

The if keyword marks the beginning of the if-else condition statement. It is followed by a space, an opening square bracket [ , and another space. Inside these brackets, you write the condition that needs to be evaluated.

The condition can be any valid expression or comparison that evaluates to either true or false. For example, you can check if a variable is equal to a specific value, compare two numbers, or check for file existence.

After defining the condition, you close it with a closing square bracket ] followed by a space.

Next, you use the then keyword to mark the beginning of code block that should be executed if the condition evaluates to true. The code block can consist of one or more commands or statements.

Following the code block for true condition execution, you use the else keyword to mark the beginning of code block that should be executed if the condition evaluates to false. Again, this code block can consist of one or more commands or statements.

Finally, you close the entire if-else statement with fi, which stands for “end of if.”

Now that we have understood the syntax, let’s see some examples to solidify our understanding.

Example 1:

Suppose we want to check if a number is positive or negative. We can write a shell script as follows:

#!/bin/bash

read -p "Enter a number: " num

if [ $num -gt 0 ]
then
    echo "The number is positive"
else
    echo "The number is negative"
fi

In this example, the user is prompted to enter a number. The script then checks if the number is greater than zero using the condition [ $num -gt 0 ].

If the condition is true, it prints “The number is positive.” Otherwise, it prints “The number is negative.”

Example 2:

Let’s consider another example where we want to check if a file exists or not. We can use the following script:

filename=”example.txt”

if [ -f $filename ]
then
echo “$filename exists.”
else
echo “$filename does not exist.”
fi

In this example, we have assigned a filename to the variable filename. The script checks if the file exists using the condition [ -f $filename ]. If the condition is true, it displays that the file exists; otherwise, it displays that the file does not exist.

Conclusion:

In this tutorial, we learned how to write an if-else condition in shell scripting. We explored the syntax and saw examples of how to use conditions in different scenarios.

Shell scripting allows us to automate tasks and make our lives easier by executing commands based on specific conditions. With practice and creativity, you can leverage if-else conditions to build powerful scripts that automate complex workflows.

Remember to experiment with different conditions and commands to become comfortable with shell scripting. Happy coding!

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

Privacy Policy