Skip to content

Practice Exercise: Using the Bash Case Statement

Objectives

  • Explore the Bash case statement for conditional branching in scripts.
  • Learn how to use the case statement to simplify decision-making in Bash scripts.
  • Create Bash scripts that utilize the case statement to perform different actions based on user input.

Scenario

The case statement is a powerful tool for conditional branching in Bash scripts. It allows you to execute different code blocks based on the value of a variable or user input. In this exercise, you'll practice using the case statement to make decisions and perform various actions within your scripts.

Tasks

Task 1: Basic Case Statement

  • Create a Bash script named basic_case.sh.
  • Implement a case statement that:
  • Prompts the user to enter a day of the week (e.g., Monday, Tuesday).
  • Based on the user input, displays a message indicating whether it's a weekday or a weekend day.
  • Account for variations in user input (e.g., case-insensitive).
  • Test your script with different day inputs to ensure it works as expected.
  • Include comments in your script to explain the case statement's structure and how it handles user input.
    #!/bin/bash
    
    # Prompt the user to enter a day of the week
    read -p "Enter a day of the week: " day
    
    # Convert the input to lowercase for case-insensitive matching
    day_lower=$(echo "$day" | tr '[:upper:]' '[:lower:]')
    
    # Use a case statement to check the day
    case "$day_lower" in
      "monday" | "tuesday" | "wednesday" | "thursday" | "friday")
        echo "$day is a weekday."
        ;;
      "saturday" | "sunday")
        echo "$day is a weekend day."
        ;;
      *)
        echo "Invalid input. Please enter a valid day of the week."
        ;;
    esac
    
  • Test and run
    [intern@intern-a1t-inf-lnx1 ~]$ chmod +x basic_case.sh
    [intern@intern-a1t-inf-lnx1 ~]$ ./basic_case.sh
    Enter a day of the week: monday
    monday is a weekday.
    [intern@intern-a1t-inf-lnx1 ~]$ ./basic_case.sh
    Enter a day of the week: sunday
    sunday is a weekend day.
    [intern@intern-a1t-inf-lnx1 ~]$ ./basic_case.sh
    Enter a day of the week: notvalid
    Invalid input. Please enter a valid day of the week.
    

Task 2: Advanced Case Statement

  • Create a Bash script named advanced_case.sh.
  • Implement a more complex case statement that:
  • Prompts the user to select an action (e.g., open, edit, delete).
  • Based on the action, asks the user for a filename.
  • Performs the selected action (e.g., display a message with the chosen action and filename).
  • Handles various actions and variations in user input.
  • Test your script thoroughly to ensure it handles different actions and filenames correctly.
  • Include detailed comments to explain the logic of your advanced case statement.
    #!/bin/bash
    
    # Prompt the user to select an action
    read -p "Select an action (open, edit, delete): " action
    
    # Convert the action input to lowercase for case-insensitive matching
    action_lower=$(echo "$action" | tr '[:upper:]' '[:lower:]')
    
    # Use a case statement to handle different actions
    case "$action_lower" in
      "open")
        # If the action is "open," prompt for a filename and display the action and filename
        read -p "Enter the filename to open: " filename
        echo "Opening file: $filename"
        vim $filename
        ;;
      "edit")
        # If the action is "edit," prompt for a filename and display the action and filename
        read -p "Enter the filename to edit: " filename
        echo "Editing file: $filename"
        vim $filename
        ;;
      "delete")
        # If the action is "delete," prompt for a filename and display the action and filename
        read -p "Enter the filename to delete: " filename
        echo "Deleting file: $filename"
        rm $filename
        ;;
      *)
        # If the action is not recognized, display an error message
        echo "Invalid action. Please select from open, edit, or delete."
        ;;
    esac
    
  • Test and run
    [intern@intern-a1t-inf-lnx1 ~]$   chmod +x advanced_case.sh
    [intern@intern-a1t-inf-lnx1 ~]$   ./advanced_case.sh
    Select an action (open, edit, delete): edit
    Enter the filename to edit: employees.csv
    Editing file: employees.csv
    

Conclusion

In this exercise, you've explored the Bash case statement for conditional branching in scripts. You've practiced using case to make decisions and perform various actions based on user input. Understanding how to implement case statements will help you create more versatile and user-friendly Bash scripts.