Input A Menu Selection In A Variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Input A Menu Selection In A Variable
# 1  
Old 04-09-2010
Input A Menu Selection In A Variable

hey all, me again....having a problem with my code, where I essentially am trying to show a menu, have the user select an option (from 1 to 5), then display which selection they picked...


Code:
#!/bin/bash

# A LIST OF THE ERROR MESSAGES AND THE PROPER SYNTAX:
error_0="Correct amount of arguments provided."
error_10="Error: No arguments present at all."
error_11="You must input the first argument."
error_12="Error: Expecting \"-i\" for the first argument."
error_13="You must enter a filename for the second argument."
error_14="$2 does not exist.  Please enter a real file."
error_15="You must enter a third argument."
error_16="Error: Expecting \"-o\" for the third argument."
error_17="You must enter a filename for the fourth argument."
error_18="Error: Too many arguments."
error_19="No overwrite selected...exiting program"

proper="Usage:  ./Lab_14.sh -i <infile> -o <outfile> \n \n-i <infile>   The filename containing the existing data. \n-o <outfile>  The filename you want the output"

hit_enter="Press <ENTER> to continue"
ow="1"
menu="1"
menu_choice="Menu option "$choice" selected"
menu_content="\n     MAIN MENU \n     --------- \n     1) Menu Option 1 \n    2) Menu Option 2 \n    3) Menu Option 3 \n    4) Menu Option 4 \n \n     5) Quit \n \n     Please enter a menu option:"
			

#CHECK IF THERE ARE NO ARGUMENTS PRESENT:
if [ "$#" == "0" ] ;then
	echo
	echo $error_10
	echo
	echo -e $proper
	echo
	exit 10
fi

#CHECK IF FIRST ARGUMENT IS BLANK:
if [ "$1" == "" ] ;then
	echo
	echo $error_11
	echo
	echo -e $proper
	echo
	exit 11
fi 

#CHECK IF FIRST ARGUMENT IS -i:
if [ "$1" != "-i" ] ;then
	echo
	echo $error_12
	echo
	echo -e $proper
	echo
	exit 12
fi

#CHECK IF SECOND ARGUMENT IS BLANK:
if [ "$2" == "" ] ;then
	echo
	echo $error_13
	echo
	echo -e $proper
	echo 
	exit 13
fi

#CHECK IF SECOND ARGUMENT (INPUT FILENAME) EXISTS:
if [ ! -e "$2" ] ;then
	echo
	echo $error_14
	echo
	echo -e $proper
	echo
	exit 14
fi

#CHECK IF THIRD ARGUMENT IS BLANK:
if [ "$3" == "" ] ;then
	echo
	echo $error_15
	echo
	echo -e $proper
	echo
	exit 15
fi

#CHECK IF THIRD ARGUMENT IS -o:
if [ "$3" != "-o" ] ;then
	echo
	echo $error_16
	echo
	echo -e $proper
	echo
	exit 16
fi

#CHECK IF FOURTH ARGUMENT IS BLANK:
if [ "$4" == "" ] ;then
	echo
	echo $error_17
	echo
	echo -e $proper
	echo
	exit 17
fi

#CHECK IF THERE ARE TOO MANY (MORE THAN 4) ARGUMENTS:
if [ "$#" -ge "5" ] ;then
	echo
	echo $error_18
	echo
	echo -e $proper
	echo
	exit 18
fi

#IF FOURTH FILE EXISTS, ASK IF USER WANTS IT OVERWRITTEN:
if [ "$1" == "-i" ] && [ -e "$2" ] && [ "$3" == "-o" ] && [ -e "$4" ] ;then
	while [ $ow != "5" ]
	do
		echo
		echo "File \"$4\" exists already"
		echo -n "Do you wish to overwrite file contents (y/n)?"
		read overwrite
	
		case $overwrite in
		y)
	        ow="5"	
			clear
			while [ $menu != "quit" ] 
			do 
				echo
				echo -en $menu_content
				read choice
	
				case $choice in
				1)
					echo 
					echo $menu_choice
					echo $hit_enter
					read continue
					clear
				;;
				2)
					echo
					echo $menu_choice
					echo $hit_enter
					read continue
					clear
				;;
				3)
					echo
					echo $menu_choice
					echo $hit_enter
					read continue
					clear
				;;
				4)
					echo
					echo $menu_choice
					echo $hit_enter
					read continue
					clear
				;;
				5)
					echo
					echo $menu_choice
					echo $hit_enter
					read continue
					clear 
					menu="quit"
					exit 0
				;;
				*)	
					echo
					echo "Try picking an option from the menu"
					echo $hit_enter
					read continue
					clear
				;;
				esac
    		done
		;;
		n)
			echo
			echo $error_19
			exit 19
		;;
		*)
			echo "Please select y or n"
			echo $hit_enter
			read continue
		;;
		esac
	done
fi

#IF FOURTH FILE DOES NOT EXIST, GO STRAIGHT TO MENU:
if [ "$1" == "-i" ] && [ -e "$2" ] && [ "$3" == "-o" ] && [ ! -e "$4" ] ;then
	clear
	while [ $menu != "quit" ] 
	do 
		echo
		echo -en $menu_content
		read choice
	
		case $choice in
		1)
			echo 
			echo $menu_choice
			echo $hit_enter
			read continue
			clear
		;;
		2)
			echo
			echo $menu_choice
			echo $hit_enter
			read continue
			clear
		;;
		3)
			echo
			echo $menu_choice
			echo $hit_enter
			read continue
			clear
		;;
		4)
			echo
			echo $menu_choice
			echo $hit_enter
			read continue
			clear
		;;
		5)
			echo
			echo $menu_choice
			echo $hit_enter
			read continue
			clear 
			menu="quit"
			exit 0
		;;
		*)	
			echo
			echo "Try picking an option from the menu"
			echo $hit_enter
			read continue
			clear
		;;
		esac
    done
fi


I bolded/italicized/underlined the problem

Whenever I make a choice in the menu, rather then saying "Menu option 1 selected" it just says "menu option selected"
# 2  
Old 04-11-2010
Hi,

Reassign the new choice to the variable menu_choice before the echo commands:
Code:
menu_choice="Menu option "$choice" selected"
echo $menu_choice

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with 'select' for menu input

A lot of my scripting makes use of the 'select' command to create menu driven input. A typical example of how I use it is as: somevar='' PS3='Select one: ' while ]; do select somevar in $(sqlplus -s $dbuser/$dbpw@mydb <<EOF set echo off feedback off verify off... (7 Replies)
Discussion started by: edstevens
7 Replies

2. Shell Programming and Scripting

Dialog --menu limitation for the entries for selection

Hi Guys, Please pardon me if this is a silly question, but I have tried to find an answer and going through the man page and failed to get one. I have created a menu driven script with dialog --menu option. Everything is working as expeted, however, it seems that if I have more than 10... (1 Reply)
Discussion started by: ww889
1 Replies

3. Shell Programming and Scripting

Php assign selection to a variable

can someone help me assign the selection of a drop down menu to a variable? the variable i need to store the selection in is inputA. <form name="inputA"> <p align="center"> <p align="center">Name: <input name="inputA" type="text" size="45"> <select> <?php if ($handle =... (1 Reply)
Discussion started by: SkySmart
1 Replies

4. UNIX for Dummies Questions & Answers

Dynamic menu selection? Help..

Hi guys, i would like to create a program that allow user to show the information of certain thing such as network card. I would like the menu to be dynamic, for example: my computer system have 2 network card inserted at the moment, therefore the menu will have 2 choice for the user. eth0... (12 Replies)
Discussion started by: malfolozy
12 Replies

5. Shell Programming and Scripting

A selection menu in a shell script

I'm writing a shell script and have a problem with selection when I issue the command, is there a way to automatically choose a selection number one after a selection menue appear Command 1-choice 2- choice 3-choice Thanks Sara (3 Replies)
Discussion started by: Sara_84
3 Replies

6. Shell Programming and Scripting

reprint the select menu after a selection

Hi, I am using a select in ksh for a script #!/bin/ksh FIRSTLIST='one two three four quit' PS3='Please select a number: ' select i in $FIRSTLIST ; do case $i in one) print 'this is one' ;; two) print 'this is 2' ;; three) print 'this is 3' ;; four) print... (7 Replies)
Discussion started by: omerzzz
7 Replies

7. Linux

Opt out of selection menu?

When I logon to my server with PuTTY I am forced into a selection menu with few options. How to I opt out of this menu to get to the home directory to view all of the files on the server? Thanks for the help. :o (1 Reply)
Discussion started by: ksirimarco
1 Replies

8. Shell Programming and Scripting

piping ls to zenity and returning selection to variable

Hi I'm fairly new to unix and scripting (in BASH) and am here to post my first question. What I'm trying to do is pipe a sorted listing (ls) to zenity and when the list appears to the user, the choice they choose is returned to a variable so it can be used in conditional statements. I can... (2 Replies)
Discussion started by: Vayshan
2 Replies

9. Shell Programming and Scripting

Variable Selection Operation Error

Ok i have taken your advised indented my code and i have managed to fix my problem but unfortuantely now another small one has arisen. The problem is that executing my commands requires two presses of the ENTER key as opposed to the originally being pressed once as one would expect, for example... (1 Reply)
Discussion started by: warlock129
1 Replies

10. Shell Programming and Scripting

Menu help with array selection

Hi there all I got the following I got multiple arrays named for example STAT_AAAA STAT_AAAB STAT_AAAC STAT_AAAD Now what I want I have chosen an option in a menu to select 1 but I dont want to write for all the same thing so I made it a signle one now what I want is to get STAT_ and... (6 Replies)
Discussion started by: draco
6 Replies
Login or Register to Ask a Question