WCC logo

CIS120Linux Fundementals

if, elif Statements

In Bash scripting, if statements are used to test conditions and make decisions based on the results. They help control the flow of execution in a script.

Basic if Statement

A basic if statement in Bash follows this syntax:

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

Example:

#!/bin/bash

number=5

if [ $number -gt 3 ]; then
    echo "The number is greater than 3"
fi

In this example, the script checks if the variable number is greater than 3 and prints a message if the condition is true.

if-else Statement

An if-else statement allows you to execute different commands based on whether the condition is true or false.

if [ condition ]; then
    # Commands to execute if condition is true
else
    # Commands to execute if condition is false
fi

Example:

#!/bin/bash

number=2

if [ $number -gt 3 ]; then
    echo "The number is greater than 3"
else
    echo "The number is not greater than 3"
fi

In this example, the script prints a different message based on whether the number is greater than 3.

Multiple if-else Statements (elif)

You can use multiple if-else statements to test several conditions.

if [ condition1 ]; then
    # Commands to execute if condition1 is true
elif [ condition2 ]; then
    # Commands to execute if condition2 is true
else
    # Commands to execute if neither condition1 nor condition2 is true
fi

Example:

#!/bin/bash

number=3

if [ $number -gt 3 ]; then
    echo "The number is greater than 3"
elif [ $number -eq 3 ]; then
    echo "The number is equal to 3"
else
    echo "The number is less than 3"
fi

In this example, the script checks multiple conditions and prints the appropriate message based on the value of number.

Nested if-else Statements

Nested if-else statements are if-else statements within another if-else statement.

Example:

#!/bin/bash

number=4

if [ $number -gt 0 ]; then
    if [ $number -lt 10 ]; then
        echo "The number is between 1 and 9"
    else
        echo "The number is 10 or greater"
    fi
else
    echo "The number is zero or negative"
fi

In this example, the script first checks if the number is greater than 0. If true, it then checks if the number is less than 10 and prints the appropriate message based on the nested conditions.

Comparisons in Bash

In Bash, you can use various comparison operators to test conditions.

Equality (==):

if [ "$str1" == "$str2" ]; then
    echo "Strings are equal"
fi

Regex Match (=~):

if [[ "$str" =~ ^[0-9]+$ ]]; then
    echo "String contains only digits"
fi

Inequality (!=):

if [ "$str1" != "$str2" ]; then
    echo "Strings are not equal"
fi

Numeric Comparisons:

Greater than (-gt):

if [ $num1 -gt $num2 ]; then
    echo "$num1 is greater than $num2"
fi

Less than (-lt):

if [ $num1 -lt $num2 ]; then
    echo "$num1 is less than $num2"
fi

Greater than or equal to (-ge):

if [ $num1 -ge $num2 ]; then
    echo "$num1 is greater than or equal to $num2"
fi

Less than or equal to (-le):

if [ $num1 -le $num2 ]; then
    echo "$num1 is less than or equal to $num2"
fi

Equal to (-eq):

if [ $num1 -eq $num2 ]; then
    echo "$num1 is equal to $num2"
fi

Not equal to (-ne):

if [ $num1 -ne $num2 ]; then
    echo "$num1 is not equal to $num2"
fi

Checks if a file exits using (-f). If so then outputs the contents, if not then says "No file found".

if [ -f file.txt ]; then
    echo "File contents:"
    cat file.txt
else
    echo "No file found."
fi

Logical Operators

You can also use logical operators to combine multiple conditions.

AND (&&):

if [ $num -gt 0 ] && [ $num -lt 10 ]; then
    echo "The number is between 1 and 9"
fi

OR (||):

if [ $num -eq 0 ] || [ $num -eq 10 ]; then
    echo "The number is 0 or 10"
fi

String Comparisons

String comparisons in Bash can also be performed using various operators:

Check if a string is empty (-z):

if [ -z "$str" ]; then
    echo "String is empty"
fi

Check if a string is not empty (-n):

if [ -n "$str" ]; then
    echo "String is not empty"
fi

Check if a string starts with a specific substring:

if [[ "$str" == prefix* ]]; then
    echo "String starts with 'prefix'"
fi

Comparison Operators Table

Comparison Operator Example Description
Equality == [ "$str1" == "$str2" ] Strings are equal
Regex Match =~ [[ "$str" =~ ^[0-9]+$ ]] String matches regex
Inequality != [ "$str1" != "$str2" ] Strings are not equal
Numeric Greater -gt [ $num1 -gt $num2 ] num1 is greater than num2
Numeric Less -lt [ $num1 -lt $num2 ] num1 is less than num2
Numeric Greater or Equal -ge [ $num1 -ge $num2 ] num1 is greater than or equal to num2
Numeric Less or Equal -le [ $num1 -le $num2 ] num1 is less than or equal to num2
Numeric Equal -eq [ $num1 -eq $num2 ] num1 is equal to num2
Numeric Not Equal -ne [ $num1 -ne $num2 ] num1 is not equal to num2
String Empty -z [ -z "$str" ] String is empty
String Not Empty -n [ -n "$str" ] String is not empty
String Starts With == prefix* [[ "$str" == prefix* ]] String starts with 'prefix'
Checks if file exits -f [[ -f file.txt ]] Checks if file.txt exits
Logical AND && [ $num -gt 0 ] && [ $num -lt 10 ] Both conditions are true
Logical OR || [ $num -gt 10 ] || [ $num -lt 5 ] Only one condition needs to be true for this condtion to evaluate as true

Summary

Bash provides powerful constructs to handle conditional execution using if, if-else, and nested if-else statements. Various comparison operators like ==, =~, !=, -gt, -lt, and logical operators && and || help in forming complex conditions. Understanding these constructs and operators allows you to write more efficient and readable Bash scripts.