MC, 2025
Ilustracja do artykułu: How to Create a Menu in Bash Script: A Step-by-Step Guide

How to Create a Menu in Bash Script: A Step-by-Step Guide

If you have ever wanted to automate tasks or create interactive programs in a Linux environment, then Bash scripting is likely a tool you've encountered. One of the most useful features of a Bash script is the ability to create interactive menus. These menus allow users to select options, making your script more user-friendly and flexible. Whether you're creating a simple tool for personal use or developing a more complex script for automation, a menu in your Bash script can make a world of difference.

What is Bash and Why Use It?

Bash, which stands for "Bourne Again Shell," is a command-line interface that allows users to interact with their system through text commands. It is widely used in Linux and macOS environments but can also be used on Windows through tools like Git Bash or Windows Subsystem for Linux (WSL).

By writing scripts in Bash, users can automate repetitive tasks, manage files, configure systems, and even create programs that execute complex sequences of commands. One of the best things about Bash is its simplicity and power, making it an essential tool for system administrators, developers, and enthusiasts alike.

Why Create a Menu in Bash?

Creating a menu in a Bash script offers several advantages. It allows users to select options from a list, which can be particularly useful for tasks that require multiple steps or choices. A well-designed menu can guide users through complex tasks without them needing to remember command syntax or parameters. It can also help prevent errors by limiting user input to valid options.

Some use cases for a Bash menu include:

  • Creating installation scripts for software packages
  • Automating system maintenance tasks
  • Providing options to start, stop, or restart services
  • Creating simple user interfaces for data processing scripts

Basic Structure of a Menu in Bash

Before diving into examples, let's explore the basic structure of a Bash menu. A typical Bash menu includes:

  • Display a list of options: This is the list of choices that the user will be able to select from.
  • Capture user input: The script waits for the user to enter a number or letter corresponding to the option they want to choose.
  • Perform actions based on the input: Depending on the user's choice, the script will execute a specific command or series of commands.
  • Repeat or exit: After performing the action, the menu can either repeat for additional choices or exit based on the user's input.

Basic Example of a Menu in Bash

Let's start with a simple Bash menu that provides three options: viewing system information, listing files, and exiting the script.

#!/bin/bash

while true; do
  echo "Please select an option:"
  echo "1. View system info"
  echo "2. List files"
  echo "3. Exit"
  read -p "Enter your choice: " choice

  case $choice in
    1)
      echo "System Information:"
      uname -a
      ;;
    2)
      echo "Listing files:"
      ls -l
      ;;
    3)
      echo "Exiting..."
      break
      ;;
    *)
      echo "Invalid choice, please try again."
      ;;
  esac
done

In this script:

  • The while true loop ensures that the menu keeps repeating until the user selects the option to exit.
  • The read command captures the user's input and stores it in the choice variable.
  • The case statement is used to perform different actions based on the user's input. If the user chooses option 1, system information is displayed. Option 2 lists the files in the current directory, and option 3 exits the script.

Advanced Example: Adding More Options

Let's add more functionality to our menu. In this advanced example, we'll include options for checking disk space, displaying the current date, and creating a new directory.

#!/bin/bash

while true; do
  echo "Please select an option:"
  echo "1. View system info"
  echo "2. List files"
  echo "3. Check disk space"
  echo "4. Show current date"
  echo "5. Create a new directory"
  echo "6. Exit"
  read -p "Enter your choice: " choice

  case $choice in
    1)
      echo "System Information:"
      uname -a
      ;;
    2)
      echo "Listing files:"
      ls -l
      ;;
    3)
      echo "Checking disk space:"
      df -h
      ;;
    4)
      echo "Current date:"
      date
      ;;
    5)
      read -p "Enter directory name: " dirname
      mkdir "$dirname"
      echo "Directory '$dirname' created."
      ;;
    6)
      echo "Exiting..."
      break
      ;;
    *)
      echo "Invalid choice, please try again."
      ;;
  esac
done

This script introduces the following new features:

  • Disk space check: The df -h command shows the available disk space in a human-readable format.
  • Current date: The date command displays the current date and time.
  • Create a new directory: The script prompts the user for a directory name and then creates it using the mkdir command.

Adding Input Validation and Error Handling

As we build more complex menus, it's important to add input validation to ensure that the user enters a valid choice. Let's enhance our previous example by adding input validation for the directory creation process.

#!/bin/bash

while true; do
  echo "Please select an option:"
  echo "1. View system info"
  echo "2. List files"
  echo "3. Check disk space"
  echo "4. Show current date"
  echo "5. Create a new directory"
  echo "6. Exit"
  read -p "Enter your choice: " choice

  case $choice in
    1)
      echo "System Information:"
      uname -a
      ;;
    2)
      echo "Listing files:"
      ls -l
      ;;
    3)
      echo "Checking disk space:"
      df -h
      ;;
    4)
      echo "Current date:"
      date
      ;;
    5)
      read -p "Enter directory name: " dirname
      if [ -d "$dirname" ]; then
        echo "Directory already exists."
      else
        mkdir "$dirname"
        echo "Directory '$dirname' created."
      fi
      ;;
    6)
      echo "Exiting..."
      break
      ;;
    *)
      echo "Invalid choice, please try again."
      ;;
  esac
done

In this updated script, the if statement checks whether the specified directory already exists. If it does, the script informs the user; if not, it creates the directory.

Conclusion: Making Bash Menus Even Better

Creating menus in Bash scripts is a powerful way to make your scripts interactive and user-friendly. By using the examples provided in this article, you can create simple or advanced menus to suit your needs. Bash scripting is an invaluable skill, and knowing how to create menus can make your scripts even more versatile.

So, next time you're automating tasks or creating an interactive script, try incorporating a menu to enhance the user experience. With a little practice, you'll be able to create complex, efficient, and well-structured Bash scripts that are easy to use and understand. Happy scripting!

Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!

Imię:
Treść: