Shell Scripting Tutorial for Beginners 5 - If Statement ( If then , If then else, If elif else)

3 min read 4 hours ago
Published on Nov 06, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through the basics of using if statements in shell scripting. Conditionals in shell scripts allow you to make decisions based on the evaluation of expressions, which is essential for controlling the flow of your scripts. Understanding how to write if, then, else, and elif statements will enhance your scripting skills and enable you to create more dynamic and responsive scripts.

Step 1: Understanding Expressions

Expressions are the foundation of conditionals and can be categorized into various types:

String Comparisons

  • =: Compare if two strings are equal.
  • !=: Compare if two strings are not equal.
  • -n: Check if string length is greater than zero.
  • -z: Check if string length is equal to zero.

Examples:

[ s1 = s2 ]   # True if s1 is the same as s2
[ s1 != s2 ]  # True if s1 is not the same as s2
[ -n s1 ]     # True if s1 is not empty
[ -z s2 ]     # True if s2 is empty

Number Comparisons

  • -eq: Compare if two numbers are equal.
  • -ne: Compare if two numbers are not equal.
  • -gt: Check if one number is greater than another.
  • -lt: Check if one number is less than another.
  • -ge: Check if one number is greater than or equal to another.
  • -le: Check if one number is less than or equal to another.

Examples:

[ n1 -eq n2 ]  # True if n1 is the same as n2
[ n1 -ne n2 ]  # True if n1 is not the same as n2
[ n1 -gt n2 ]  # True if n1 is greater than n2
[ n1 -lt n2 ]  # True if n1 is less than n2

Step 2: Writing Basic If Statements

Now that you understand expressions, let's write some basic if statements.

Syntax of If Statements

if [ condition ]; then
    # commands to execute if condition is true
fi

Example

if [ -f "myfile.txt" ]; then
    echo "File exists."
fi

Step 3: Using If Then Else

You can extend your if statements with an else clause to handle false conditions.

Syntax of If Then Else

if [ condition ]; then
    # commands if condition is true
else
    # commands if condition is false
fi

Example

if [ -f "myfile.txt" ]; then
    echo "File exists."
else
    echo "File does not exist."
fi

Step 4: Using If Elif Else

For multiple conditions, you can use elif to check additional conditions.

Syntax of If Elif Else

if [ condition1 ]; then
    # commands if condition1 is true
elif [ condition2 ]; then
    # commands if condition2 is true
else
    # commands if none are true
fi

Example

if [ $num -gt 10 ]; then
    echo "Number is greater than 10."
elif [ $num -eq 10 ]; then
    echo "Number is equal to 10."
else
    echo "Number is less than 10."
fi

Conclusion

In this tutorial, you learned how to use if statements in shell scripting, including the use of string and number comparisons. You also practiced writing if, then, else, and elif statements to control the flow of your scripts.

Next steps could include diving deeper into nested conditionals or exploring loops to further enhance your scripting capabilities. Happy scripting!