Creating Menu in Unix Shell Programming

Introduction

This tutorial will show you how to create menu in Unix Shell Programming. You 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, I 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 (menu.sh) 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 I 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 I print stars at the next line and after that I have five options in the menu system. Then again I print stars followed by new line.

Then I 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.

I 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.

Testing the Program

Make sure you update the execute permission for the shell script file menu.sh by executing the following command:

$ chmod +x menu.sh

Executing the above shell script will give you the following output. You can enter the menu option and perform different operations, such as, add, substract, multiply and divide.

You can use the command $ ./menu.sh to run the script.

menu in unix shell

Source Code

Download

Leave a Reply

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