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.

Menu system may be created either in graphical user interface or command line interface. Here we will be creating menu in unix interface, so it’s command line interface. Here the menu in unix basically presents options to the end user to perform tasks and until user exits from the system the menu options will be repeated for performing the same tasks.

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

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 any one of the 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 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.

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

Here in the above program -r -p means display prompt for input without new line. So read the input from keyboard, i.e., user. We have also given variable c at the rightmost place to capture the input into c variable.

You may add or remove options from the above menu system. Even you can create your completely new menu in unix shell script using the same cocept covered in the above program.

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

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

Leave a Reply

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