Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


CompTIA Linux+ XK0-005 - 3.1 - Shell Script Elements: if Statement

if statements are fundamental constructs in shell scripting that allow you to make decisions based on conditions. With if statements, you can execute different blocks of code depending on whether a condition is true or false. This control flow mechanism is essential for creating dynamic and conditional scripts.

Basic Syntax

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

if condition1
then
    # Code to be executed if condition1 is true
elif condition2
then
    # Code to be executed if condition1 is false and condition2 is true
else
    # Code to be executed if both condition1 and condition2 are false
fi
  • condition1, condition2, etc., are expressions or commands that evaluate to either true or false.
  • The code block following the then keyword will execute if the corresponding condition is true.
  • Optionally, you can include multiple elif blocks to check additional conditions in a sequential manner.
  • The else block will execute if all the previous conditions are false.
  • The statement ends with the fi keyword.

Example: Checking a Number

#!/bin/bash

number=15

if [ $number -eq 0 ]
then
    echo "The number is zero."
elif [ $number -gt 0 ]
then
    echo "The number is positive."
else
    echo "The number is negative."
fi

In this example, the if statement checks the value of the number variable. The first condition [ $number -eq 0 ] checks if the number is equal to zero. If the condition is true, the script prints that the number is zero. If the condition is false, it moves to the next elif condition [ $number -gt 0 ] to check if the number is greater than zero. If this condition is true, the script prints that the number is positive. If both conditions are false, the script executes the else block and prints that the number is negative.

Example: Checking a File Extension

#!/bin/bash

filename="script.sh"

if [ "${filename##*.}" = "sh" ]
then
    echo "The file is a shell script."
elif [ "${filename##*.}" = "txt" ]
then
    echo "The file is a text file."
else
    echo "The file type is unknown."
fi

In this example, the if statement examines the file extension of the filename variable. The conditions [ "${filename##*.}" = "sh" ] and [ "${filename##*.}" = "txt" ] use substring manipulation to extract the file extension and compare it to known extensions. If the first condition is true, the script prints that the file is a shell script. If the second condition is true, it prints that the file is a text file. If both conditions are false, the script executes the else block and prints that the file type is unknown.

Conclusion

if statements provide a way to incorporate conditional logic into your shell scripts. By understanding the basic syntax and the usage of elif statements, you can create scripts that make decisions based on multiple conditions.

Remember to customize the conditions and code blocks within the if statements to suit your specific requirements. You can have as many elif blocks as necessary to check additional conditions. The code blocks within the if, elif, and else sections should contain the appropriate commands or operations to be executed based on the conditions.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.