Open In App

Menu-Driven Shell Script

Last Updated : 02 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Here we are going to discuss how to create a menu-driven shell script. Let’s first understand what does menu-driven means and why should we learn to create a menu-driven shell script. Basically, a menu-driven shell script provides users more options/interactive interface. In a layman’s term shell script is similar to the restaurant menu, suppose you are at your favorite restaurant, and you asked for a restaurant menu, so you can choose your favorite dish. Similarly, a menu-driven shell script serves the same purpose.

Let’s take an example for better understanding:

Using case esac statement to create a menu-driven shell script:

A case statement is similar to a switch statement from another languages. Case statement is an alternative to multilevel if-then-else statements. Using case statement we can avoid using multiple if-then-else and reduce script size. Case is used to match several value against one value.

Syntax: case $condition in

           pattern1 Statement(s) to be executed if pattern1 matches;;

          pattern2 Statement(s) to be executed if pattern2 matches;;

  *)  Default case;;

esac

Here, case is keyword and $condition is the input value that we match again several another values. Pattern can be anything, it can be alphabets, integers or regexp. A pattern is terminated with the closing parenthesis ‘)‘. If the pattern match with the input value then statements followed by the pattern will be executed and the statements are terminated by two semicolons ‘;;‘. *) is default case i.e. if user input does not match any of the given pattern the statements followed by this pattern will be executed. To end the case statement esac keyword is used. It is the reverse of ‘case’ keyword.

Steps included :

  1. Create a custom menu using echo statement and show the menu
  2. Create an infinite loop using while statement that accept the user input option and generate the output continuously until the user input matches the exit pattern.
  3. Take input from the user using read statement and store it in a variable.
  4. Use case statement to check if the input matches with the pattern.
  5. Create custom pattern.
  6. Exit the case statement using esac keyword.
#!/bin/bash

# creating a menu with the following options
echo "SELECT YOUR FAVORITE FRUIT";
echo "1. Apple"
echo "2. Grapes"
echo "3. Mango"
echo "4. Exit from menu "
echo -n "Enter your menu choice [1-4]: "

# Running a forever loop using while statement
# This loop will run until select the exit option.
# User will be asked to select option again and again
while :
do

# reading choice
read choice

# case statement is used to compare one value with the multiple cases.
case $choice in
  # Pattern 1
  1)  echo "You have selected the option 1"
      echo "Selected Fruit is Apple. ";;
  # Pattern 2
  2)  echo "You have selected the option 2"
      echo "Selected Fruit is Grapes. ";;
  # Pattern 3
  3)  echo "You have selected the option 3"
      echo "Selected Fruit is Mango. ";;    
  # Pattern 4
  4)  echo "Quitting ..."
      exit;;
  # Default Pattern
  *) echo "invalid option";;
  
esac
  echo -n "Enter your menu choice [1-4]: "
done

Output:

In this script, we have created a simple menu using echo statements. while : do is used to create an infinite loop to ask input from user continuously, this while loop will terminate when user input option 4 (Exit). In this while loop we are taking input from the user using the read keyword and store it into a choice variable. Then we are using case statement to check that is choice variable value matches with any of the given pattern.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads