Introduction

This tutorial will show you how to create menu in Unix Shell Programming. We know that menu gives user options to perform tasks smoothly because without menu user may not be able to understand what to do when a user has to choose an operation from multiple options.

Menu system may be created either in graphical user interface or command line interface.

In this example, we will be creating menu in Unix interface, so it’s a command line interface.

The menu system in Unix programming gives options to the end user to perform certain tasks and until user exits from the menu system the menu options will be repeated for performing the same tasks.

Creating Menu System

Here I have written the whole unix shell script to create the menu in unix.

The below menu system gives users options to perform four operations – addition, subtraction, multiplication and division.

Apart from above four operations we have also given user additional option to exit from the system.

echo -e #new line
echo "************"
echo "1. Add"
echo "2. Substract"
echo "3. Multiply"
echo "4. Divide"
echo "5. Exit"
echo "************"
echo -e #new line
read -r -p "Enter your choice: " c
echo -e #new line

if((c != 5))
then
 while true
 do
 case $c in
 1)
 echo "You chose to add two numbers"
 read -r -p "Enter number one: " x
 read -r -p "Enter number two: " y
 add=$((x+y))
 echo "Addition of numbers $x and $y: " $add
 ;;
 2)
 echo "You chose to subtract one number from another number"
 read -r -p "Enter number one: " x
 read -r -p "Enter number two: " y
 subtract=$((x-y))
 echo "Substraction of numbers $x and $y: " $subtract
 ;;
 3)
 echo "You chose to multiply two numbers"
 read -r -p "Enter number one: " x
 read -r -p "Enter number two: " y
 multiply=$((x*y))
 echo "Multiplication of numbers $x and $y: " $multiply
 ;;
 4)
 echo "You chose to divide one number by another number"
 read -r -p "Enter number one: " x
 read -r -p "Enter number two: " y
 divide=`echo "$x/$y" | bc -l`
 echo "Division of numbers $x and $y: " $divide
 ;;
 5)
 echo "You chose to exit"
 break
 ;;
 esac                                

 echo -e #new line
 echo "************"
 echo "1. Add"
 echo "2. Substract"
 echo "3. Multiply"
 echo "4. Divide"
 echo "5. Exit"
 echo "************"
 echo -e #new line
 read -r -p "Enter your choice: " c
 echo -e #new line
 done
fi

The first line tells to print new line before giving menu options to the users.

Then we print stars at the next line and after that we have five options in the menu system. Then again we print stars followed by new line.

Then we prompt user to input option for performing any one of the given tasks.

So if user press 1 then user chooses for addition of two numbers.

Next user will be asked for inputting two numbers.

Then finally user will get the summation of the given two numbers as a result.

The similar things happens for subtract, multiply and divide.

User may choose to exit from the system any time.

Here in the above program -r -p means display prompt for input without new line.

So read the input from keyboard, i.e., user will input through keyboard.

We have also given variable c at the rightmost place to capture the input value into c variable.

You may add or remove options from the above menu system to adjust according to your requirements.

Even you can create completely new menu system in Unix shell script using the same concept covered in the above program.

Congratulations! You have successfully created menu in unix shell programming.

Thanks for reading.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *