Home Linux Conditional Statements in Shell Script

Conditional Statements in Shell Script

by Anup Maurya
3 minutes read

Shell scripting is a powerful tool that enables users to automate repetitive tasks, manipulate files, and perform system administration tasks. One of the most essential components of shell scripting is the use of conditional statements. In this tutorial, we will explore conditional statements in shell scripting and how they are used to control the flow of a program.

Overview of Conditional Statements

Conditional statements in shell scripting are used to test whether a certain condition is true or false. Based on the outcome of the test, the script can execute different commands. The most common types of conditional statements in shell scripting are if, elif, and else.

The if statement

The if statement is used to test a single condition. The syntax of the if statement is as follows:

if condition
then
   commands
fi

In this syntax, condition is the expression that is tested, and commands are the commands that are executed if the condition is true. If the condition is false, the commands are not executed.

The elif statement

The elif statement is used to test multiple conditions. If the if statement is false, then the elif statement is tested. The syntax of the elif statement is as follows:

if condition1
then
   commands
elif condition2
then
   commands
else
   commands
fi

In this syntax, condition1 is the first expression that is tested, and commands are the commands that are executed if condition1 is true. If condition1 is false, then condition2 is tested, and if condition2 is true, commands are executed. If condition2 is false, then the else statement is executed.

The else statement

The else statement is used to execute commands if all of the conditions in the if and elif statements are false. The syntax of the else statement is as follows:

if condition
then
   commands
else
   commands
fi

In this syntax, condition is the expression that is tested, and commands are the commands that are executed if the condition is true. If the condition is false, the else statement is executed, and commands are executed.

Examples

Let’s look at some examples of how conditional statements can be used in shell scripting.

Example 1: Testing a Single Condition

#!/bin/bash

echo "Enter a number: "
read num

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

In this example, the script prompts the user to enter a number. The script then tests if the number is greater than 0. If the number is greater than 0, the script prints a message indicating that the number is positive.

Example 2: Testing Multiple Conditions

#!/bin/bash

echo "Enter a number: "
read num

if [ $num -gt 0 ]
then
   echo "$num is a positive number."
elif [ $num -lt 0 ]
then
   echo "$num is a negative number."
else
   echo "$num is zero."
fi

In this example, the script prompts the user to enter a number. The script then tests if the number is greater than 0. If the number is greater than 0, the script prints a message indicating that the number is positive. If the number is less than 0, the script prints a message indicating that the number is negative. If the number is 0, the script prints a message indicating that the number is zero.

Example 3: Testing File Existence

#!/bin/bash

echo "Enter the path to a file: "
read file

if [ -e $file ]
then
   echo "The file exists."
else
   echo "The file does not exist."
fi

In this example, the script prompts the user to enter the path to a file. The script then tests if the file exists using the -e option in the square brackets. If the file exists, the script prints a message indicating that the file exists. If the file does not exist, the script prints a message indicating that the file does not exist.

Conclusion

Conditional statements are an essential component of shell scripting that enable users to test conditions and control the flow of a program. In this tutorial, we explored the if, elif, and else statements and how they can be used in shell scripting to test conditions and execute commands based on the outcome of the tests. With this knowledge, you can now write shell scripts that are more powerful and efficient.

related posts

Leave a Comment

Enable Notifications OK No thanks