Shell Script Menu Examples

Introduction

Shell Script Menu Examples are a useful tool for automating tasks and improving productivity in Linux environments. By creating a simple menu interface for common tasks, users can save time and avoid the need for complex command line syntax. In this tutorial, we’ll explore how to create a shell script menu example step-by-step, and demonstrate how it can streamline your workflow. Let’s give it a go together…

Example: Shell Script Menu Using select

Here’s an example of how to create a simple shell script menu using the select command:

#!/bin/bash

options=("Option 1" "Option 2" "Option 3" "Quit")

select opt in "${options[@]}"
do
    case $opt in
        "Option 1")
            echo "You selected Option 1"
            ;;
        "Option 2")
            echo "You selected Option 2"
            ;;
        "Option 3")
            echo "You selected Option 3"
            ;;
        "Quit")
            break
            ;;
        *)
            echo "Invalid option selected"
            ;;
    esac
done

In this example, we first define an array called options that contains the menu options we want to display. We then use the select command to display the menu to the user and read their input.

The select command takes a prompt string as its argument, which is displayed to the user before the menu options. It then reads the user’s input and sets the value of the variable opt to the selected option.

The case statement is used to perform different actions depending on the selected option. In this example, we simply print a message to the console indicating which option was selected.

The break statement is used to exit the menu loop when the user selects the “Quit” option.

Running the Menu

To run the menu, save the script to a file (e.g. menu.sh) and make it executable:

chmod +x menu.sh

Then, run the script:

./menu.sh

This will display the menu to the user, and they can select an option by entering the corresponding number.

In addition to the select command, you can also create a shell script menu using a while loop in Bash. In this tutorial, we’ll provide an example of how to create a shell script menu using a while loop.

Example: Shell Script Menu using While Loop

Here’s an example of how to create a simple shell script menu using a while loop:

#!/bin/bash

menu_choice=""
while [ "$menu_choice" != "4" ]
do
    echo "Please select an option:"
    echo "1. Option 1"
    echo "2. Option 2"
    echo "3. Option 3"
    echo "4. Quit"

    read menu_choice

    case $menu_choice in
        1)
            echo "You selected Option 1"
            ;;
        2)
            echo "You selected Option 2"
            ;;
        3)
            echo "You selected Option 3"
            ;;
        4)
            echo "Goodbye"
            ;;
        *)
            echo "Invalid option selected"
            ;;
    esac
done

In this example, we first set the menu_choice variable to an empty string. We then use a while loop to display the menu to the user and read their input.

Inside the loop, we use the echo command to display the menu options to the user, and then use the read command to read their input into the menu_choice variable.

The case statement is used to perform different actions depending on the selected option. In this example, we simply print a message to the console indicating which option was selected.

The while loop continues to run until the user selects the “Quit” option by entering 4.

Running the Menu

To run the menu, save the script to a file (e.g. menu.sh) and make it executable:

chmod +x menu.sh

Then, run the script:

./menu.sh

This will display the menu to the user, and they can select an option by entering the corresponding number.

Also, see the example code shell-scripting-examples in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author