Menu Driven Bash Shell Script with Default Option


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Menu Driven Bash Shell Script with Default Option
# 1  
Old 06-17-2016
Menu Driven Bash Shell Script with Default Option

Hi All,

I have written a menu driven bash shell script.

Current Output is as below:

Code:
-------------------------------------
            Main Menu
-------------------------------------
[1] Option 1
[2] Option 2
[3] Option 3
[4] Option 4
[5] Exit
=====================================
Enter your menu choice [1-5]:

I am able to run the functions by selecting the options 1 to 5 accordingly.

My requirement is to keep option 1 as a default option so whenever I press "Enter" it should select option 1 automatically instead of user input.

Required output :

Code:
-------------------------------------
            Main Menu
-------------------------------------
[1] Option 1
[2] Option 2
[3] Option 3
[4] Option 4
[5] Exit
=====================================
Enter your menu choice [1-5] [Default :1]:


Current Shell Script Code:

Code:
#!/usr/bin/bash

# Menu Item

main_menu()
{

while :
do
	clear
	echo " "
	echo "-------------------------------------"
	echo "            Main Menu "
	echo "-------------------------------------"
	echo "[1] Option 1"
	echo "[2] Option 2"
	echo "[3] Option 3"
	echo "[4] Option 4"
	echo "[5] Exit"
	echo "====================================="
	echo "Enter your menu choice [1-5]: \c "
	read m_menu
	
	case "$m_menu" in

		1) option_1;;
		2) option_2;;
		3) option_3;;
		4) option_4;;
		5) exit 0;;
		*) echo "\nOpps!!! Please Select Correct Choice";
		   echo "Press ENTER To Continue..." ; read ;;
	esac
done
}

option_1()
{
  echo "option 1"
  echo "\nPress ENTER To Continue..."
    read
	return
}

option_2()
{
	echo "option 2"
	echo "\nPress ENTER To Continue..."
    read
	return
}

option_3()
{
	echo "option 3"
	echo "\nPress ENTER To Continue..."
    read
	return
}

option_4()
{
	echo "option 4"
	echo "\nPress ENTER To Continue..."
    read
	return
}

main_menu

Regards,
Kiran




Moderator's Comments:
Mod Comment Thanks for using code tags for code, but use it for data/samples/output as well!

Last edited by RudiC; 06-17-2016 at 11:13 AM.. Reason: Added code tags.
# 2  
Old 06-17-2016
Add:

Code:
"") option_1;;

Maybe you're interested in "dialog"(Needs to be installed first) too. dialog is text-based dialog.

See:
DIALOG – Script-driven curses widgets

Code:
exec 3>&1
RESULT=$(dialog --menu "Main Menu" 10 80 8 \
    1 "Option 1" \
    2 "Option 2" \
    3 "Option 3" \
        2>&1 >&3)
exec 3>&-

case "$RESULT" in 
 ...
esac


Last edited by stomp; 06-17-2016 at 11:45 AM..
This User Gave Thanks to stomp For This Post:
# 3  
Old 06-17-2016
Using bash, you might be interested in the select command provided?
This User Gave Thanks to RudiC For This Post:
# 4  
Old 06-18-2016
Thanks All. I have modified the script as required. Got the expected results now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using menu driven script

Hi Team , I wrote a shell script for adding and subtracting two numbers am getting error could some one please help to fix it script: echo "Enter 1 to add:" echo "Enter 2 to sub:" echo "Enter 3 for both addition and subtraction :" read ans; case "$ans" in 1)... (4 Replies)
Discussion started by: knz
4 Replies

2. Shell Programming and Scripting

Use of stty vs trap in script-driven login menu

My employers would like me to selectively run one of several different (already-existing) Korn Shell menu-driven scripts out of the user's .profile file, depending on some yet-to-be-specified user critieria. I've never done this kind of thing, but I have the existing scripts (among other... (5 Replies)
Discussion started by: Clovis_Sangrail
5 Replies

3. Shell Programming and Scripting

Menu driven script.

I'm a beginner at scripting and have been putting this script together over the past week. It's no where as polish as it could be. Any tips/suggestions on improving this script would be appreciate it. Every week, my team develops WAR files in tomcat on our test environment and moves them to our... (4 Replies)
Discussion started by: bouncer
4 Replies

4. Shell Programming and Scripting

Help needed in writing a menu driven script

Hi, I was wondering if someone could help me write a shell script in Linux that backsup/restores data to anywhere I choose but it needs to be menu driven? Thanks, I'm new to Linux/Unix but liking it so far...just hoping to get to grips with the scripts! :) (7 Replies)
Discussion started by: Nicole
7 Replies

5. UNIX for Dummies Questions & Answers

What is a menu or command line option driven script?

i'm confused what this means. i was asked to design a menu or command line option driven script that reads out of a DB and displays info such as read_data.pl -u <user> -e <event> which would print commands run by <user>with the <event> in the db. any suggestions? i've been using... (2 Replies)
Discussion started by: kpddong
2 Replies

6. SuSE

Write shell script using menu-driven approach to show various system

QUESTION: Write shell script using menu-driven approach to show various system configuration like 1) Currently logged user and his logname 2) Your current shell 3) Your home directory 4) Your current path setting 5) Your current working directory 6) Show Currently logged number of... (1 Reply)
Discussion started by: bboyjervis
1 Replies

7. Shell Programming and Scripting

Menu driven Script needed ..pls help

Hi Guys.. am new to unix scrpiting..I need a Menu need to create using shell scrpting eg: Food items ready paid if i press "f" need to add items for a file food items.. if i press "r" it need to move into ready and remove from food items if i press "p" need to update a filed in... (1 Reply)
Discussion started by: sasdua
1 Replies

8. Shell Programming and Scripting

Assigning values to reference variables for a dynamic menu driven script.

How do I assign values to reference variables? I am assigning a variable name to --> $user_var Then I am trying to change its underlying variable value by $((user_var))=$user_value .. its failing,, Please let me know if there is a way to do this dynamically.. FileA.props... (5 Replies)
Discussion started by: kchinnam
5 Replies

9. Homework & Coursework Questions

Menu Driven Shell Script which accepts1 to 5 options

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1) Write a Menu Driven Shell Script which accepts1 to 5 options and performs the following actions for... (1 Reply)
Discussion started by: vaghya
1 Replies

10. Shell Programming and Scripting

Unix Shell Script: With Menu Option

I am attempting to create a shell script with the following capaciblities: 1. Listed options to choice from 2. Use to perform awk statements 3. Print a report with the awk results My questions are 1. How do I select more than one file for option #5 and #6 2. How to I create an... (11 Replies)
Discussion started by: jroberson
11 Replies
Login or Register to Ask a Question