top of page

Bash Scripting: Control Flow with if and case Statements

Writer: compnomicscompnomics

Bash scripting is a powerful way to automate tasks in Linux. One of the core concepts you need to grasp is control flow, which allows your scripts to make decisions and execute different blocks of code based on conditions. Today, we'll explore the if and case statements with simple, practical examples.


The if Statement: Making Decisions

The if statement allows your script to execute a block of code only if a certain condition is true. Here's the basic syntax:


if [ condition ]; then
  # Code to execute if the condition is true
fi
  • [ condition ]: This is where you specify the condition to be evaluated. The spaces around the brackets are important!

  • then: Indicates the start of the code block to be executed if the condition is true.

  • fi: Marks the end of the if statement.


Example 1: Checking a Number

#!/bin/bash

num=10

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

Explanation:

  • We declare a variable num and assign it the value 10.

  • The condition $num -gt 5 checks if num is greater than 5. -gt stands for "greater than."

  • Since 10 is greater than 5, the message is printed.


Example 2: if-else for Two Choices

#!/bin/bash

file="myfile.txt"

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

Explanation:

  • -f $file checks if the file myfile.txt exists.

  • If it exists, the first message is printed.

  • If not, the else block is executed, and the second message is printed.


Example 3: if-elif-else for Multiple Choices

#!/bin/bash

score=75

if [ $score -ge 90 ]; then
  echo "A"
elif [ $score -ge 80 ]; then
  echo "B"
elif [ $score -ge 70 ]; then
  echo "C"
else
  echo "D"
fi

Explanation:

  • We check the score against different ranges using elif (else if).

  • The corresponding grade is printed.


The case Statement: Matching Patterns

The case statement is useful when you need to compare a variable against multiple patterns.

Bash

case $variable in
  pattern1)
    # Code to execute if $variable matches pattern1
    ;;
  pattern2)
    # Code to execute if $variable matches pattern2
    ;;
  *)
    # Code to execute if $variable matches none of the above
    ;;
esac
  • $variable: The variable to be matched.

  • pattern1, pattern2, etc.: The patterns to compare against.

  • ;;: Marks the end of a pattern's code block.

  • *): The default pattern, executed if no other pattern matches.

  • esac: Marks the end of the case statement.


Example 4: Checking a Letter

#!/bin/bash

letter="b"

case $letter in
  a)
    echo "Letter is a"
    ;;
  b)
    echo "Letter is b"
    ;;
  c)
    echo "Letter is c"
    ;;
  *)
    echo "Letter is something else"
    ;;
esac

Explanation:

  • We check the value of letter against different letters.

  • The matching message is printed.


Example 5: Checking a Day of the Week

#!/bin/bash

day="Mon"

case $day in
  Mon|Tue|Wed|Thu|Fri)
    echo "Weekday"
    ;;
  Sat|Sun)
    echo "Weekend"
    ;;
  *)
    echo "Invalid day"
    ;;
esac

Explanation:

  • We use | to specify multiple patterns in a single case.

  • The script determines if the given day is a weekday or weekend.


Key Takeaways:

  • if statements are used for making decisions based on single conditions.

  • case statements are helpful for matching a variable against multiple patterns.

  • Control flow is essential for creating dynamic and powerful bash scripts.


By mastering if and case statements, you'll be well on your way to writing more complex and useful bash scripts. Practice these examples and experiment with your own conditions and patterns to solidify your understanding.

 
 
 

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page