Sunday, March 13, 2022

Unix menu script example, shell script example for menu driven options

Below example demonstrates how to create a menu driven shell script, it allows user to select options from the menu or a list of options.

Steps to run in the unix.

1. Write or copy below program in a text editor and save as extension .sh (ex favColour.sh).

2. Make it executable (for ex chmod u+x favColour.sh, user should be able to run it)

3. Run the program by typing ./favColour.sh




#!/bin/bash

# menu driven shell script
echo "Choose your favourite colour";
echo "1. Green"
echo "2. Blue"
echo "3. Black"
echo "4. Yellow"
echo "5. Exit"
echo -n "Enter your menu choice [1-5]: "
# Running a loop until user choose to select exit
while :
do
# reading option
read option
# case statement is used to compare one value with the multiple cases.
case $option in
  # option 1
  1)  echo "You have selected the option 1"
      echo "Selected colour is Green. ";;
  # option 2
  2)  echo "You have selected the option 2"
      echo "Selected colour is Blue. ";;
  # option 3
  3)  echo "You have selected the option 3"
      echo "Selected colour is Black. ";;    
  # option 4
  4)  echo "You have selected the option 4"
      echo "Selected colour is Yellow. ";;  
  # option 5
  5)  echo "Exit ..."
      exit;;
  # Default Pattern
  *) echo "Please choose correct option";;
esac
  echo -n "Enter your option [1-5]: "

done

No comments:

Post a Comment

Set up gmail in outlook using app password

You might receive an error saying, "unable to connect to your outgoing server" while setting up a gmail account in outlook even wh...