My menu function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting My menu function
# 1  
Old 05-17-2008
My menu function

Below is my menu options code. It allows user to pick one of two option. I can get the first option to work, because if it is selected it just "breaks" and carries on with the script.

What i want to know is if a user selects option two how do i get that option to ignore all the other script and go to another specific part to carry out other commands??? ( i have indicated the problem by putting ########)

Code:
while true; do
    echo " "        
    echo "Main Menu: "
    echo "Please Select An Option Using The Options Provided."
    echo " "        
    echo "1 - Search All Files"
    echo " "        
    echo "2 - Idex Page Information"
        echo " "
        echo "Q - Quit"
        echo " "
        echo "Please enter your option: "
        read CHOSEN_KEY
        case $CHOSEN_KEY in
                [1])    echo " "
            echo "You Have Selected To Search All Files For Hits"
                        break;;                               # By using break i carry on my script which works correctly.
                [2])    echo " "
            echo "You Have Selected To Access Index Page Information"
                      ######;;
                Q|q)    echo " "
            echo "Goodbye"
                        exit;;
        esac
done


Last edited by amatuer_lee_3; 05-17-2008 at 11:14 PM..
# 2  
Old 05-17-2008
What do you mean by "make my menu run"? What errors do you get? Do you have a problem starting it like

Code:
me@mybox$ mymenu.sh

Therefore you have to set proper rights, e.g. execute-bit (chmod 755 mymenu.sh). First line of your program must be "#!/bin/bash". If your current working dir is not in your $PATH (preferred) you must start via

Code:
me@mybox$ ./mymenu.sh

# 3  
Old 05-17-2008
Code:
[2])    echo " "
			echo "You Have Selected To Access Index Page Information"
                       ###What do i put here to make it go to a new set of funtions###;;

Basically if i use this part of my script to move to another place for example the user selects option two, and i want it to output a function: so i type:

Code:
 [2])    echo " "
			echo "You Have Selected To Access Index Page Information"
                   readindex;;

###and have later in my code####

readindex () {

echo "hello"

}

it does not work

Last edited by amatuer_lee_3; 05-17-2008 at 10:40 PM..
# 4  
Old 05-17-2008
Well, might there be some difference (more specificly: a space) between the call to "readindex" and your function's name "read index"?
# 5  
Old 05-17-2008
********************************************* Thread Closed ******************************************************

Last edited by amatuer_lee_3; 05-17-2008 at 11:14 PM..
# 6  
Old 05-17-2008
Quote:
Originally Posted by amatuer_lee_3
I was wondering what i have to put to make my menu run.

What problems are you having?

Your menu runs as is (after uncommenting the double semi-colons in the case statement; see below).

You do not need a shebang at the top of the file, but you should make the script executable. (You can run it without its being executable, by calling it as a argument to sh.)
Quote:
Here is my code:

When posting code, please format it so that it will fit into a typical screen. It is best to keep the number of characters per line below 80.
Quote:
Code:
############################################################### INTRODUCTION ###########################################################################

while true; do										# While re-run function at the end of the script is true (e.g. 												# Y/y or N/n). Then do the following
echo temp >tempfile									# Output the text "temp" to the file called tempfile


Don't write useless comments. It's obvious what those lines do.
Quote:
Code:
echo "------------------------------------------------"					#
echo "*****      Welcome to the HITS search      *****"					# Welcome messages to user
echo "------------------------------------------------"					#

echo "This program will allow you to search for hits across all of your .hits files."	# Information about the program, displayed to user
echo "It will output result based on your search criteria"				#


What are ".hits files"? What do they contain? Does your search pattern match the format in the files?
Quote:
Code:
while true; do
{SNIP}
        read CHOSEN_KEY
        case $CHOSEN_KEY in
                [1])    echo " " 
			echo "You Have Selected To Search All Files For Hits"
                        break;;                               # By using break i carry on my script which is what all the rest of the code does.
                [2])    echo " "
			echo "You Have Selected To Access Index Page Information"
                       ###What do i put here to make it go to a new set of funtions###;;


You have commented out the ';;' so that the script will not run as is.

What "new set of functions" do you want to run? If it's in a separate script, just call that script; if not, you should probably use shell functions (which must be defined before they are called).

Quote:
Code:
                Q|q)    echo " "
			echo "Goodbye"
                        exit;;
        esac
done

############################################################### SEARCH MONTH ###########################################################################

while true; do										# While month function is true (e.g. Jan, Feb, Mar, etc..) 												# Then do the following
{SNIP}
done											# End function

grep $5 "$MONTH" hits/*.hits > tempfile							# Match the month variable to months in column five in any hits 											# file in the hits directory and output the results to a file 												# called tempfile.

############################################################### SEARCH DATE ############################################################################

while true; do 										# While month function is true (e.g. Entering numbers between   											# 1 & 31). Then do the following.
{SNIP}
done											# End function

grep $4 "$DATE" tempfile > tempmonth							# Match the date variable to dates in column four in the 												# tempfile file that was created after the month function and 												# output the results to a file called tempmonth.

################################################################ SEARCH YEAR ###########################################################################

while true; do										# While month function is true (e.g. 2005, 2006, or 2007..) 												# Then do the following
{SNIP}
done											# End function

grep $8 "$YEAR" tempmonth > HITS							# Match the year variable to years in column eight in the 												# tempmonth file that was created after the date function and 												# output the results to a file called tempmonth.

############################################################## OUTPUT FUNCTION #########################################################################

sleep 2											# Pause for two seconds


Why the pause?

You should make the script more user friendly by prompting for a date all in one go:

Code:
printf "%s " "Enter date in the format 'YEAR Month Day':"
read YEAR MONTH DAY

And put it and other parts of your script in their own shell functions to make the code easier to read and easier to test section by section:

Code:
getdate()
{
  printf "%s: " "Enter date in the format 'YEAR Month Day'"
  read YEAR MONTH DAY

  ## Supply functions check_year, check_month and check_date
  check_year || return 1
  check_month || return 1
  check_date || return 1
}

while true; do
  ## print menu
  printf "%s\n" "1. ..." "" "2. ..." "" "Q - Quit" ""
  printf "%s: " "Enter your choice"
  read option
  case $option in
    1) until getdate; do :; done
       ## do whatever
       ;;
    2) : ## do whatever
       ;;
    q|Q) exit
         ;; 
done

# 7  
Old 05-17-2008
please see other post:

https://www.unix.com/shell-programmin...e-revised.html

But in reply to your questions: .hits files are numerous files populated with hundres of rows in the format:

Code:
137.44.2.8 	Mon 		Feb 4 		22:02:35 GMT 2008
149.192.2.81 	Mon 		Feb 4 		23:22:12 GMT 2008
132.53.17.171 	Tue 		Feb 5 		01:56:16 GMT 2008

I have scripts that search sepearetly for the date month and year to output the results into a tempfile. Thus limiting the results from all the hits files.
I am using a grep command to do so.

Secondly I use the case command to validate all of these inputs the user makes.

The reason i have not grouped it all together, is because i dont know how. Unlike most "students" on here i tend to only use what i understand or know. compared to people who just post hwk on here, and if i need help with a command then i come to here. Although this has nothing to do with coursework, and is just a mess about task for me to help me with my placement for next year where i will be having unix and perl experience.

I am a complete beginner to this. The reason there are pauses is because its just so i can see if everything runs ok and it gives me time to look at it.

If you refer to the link above you will see the problem. I want my "option 2" in my menu to ignore the stuff i already have in place, and run something else in the same script.

Sorry to be such a lamen about this whole thing but its the first time ive extensively used UNIX.

Last edited by amatuer_lee_3; 05-17-2008 at 11:33 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Gnome 3.28.3 menu item dissapears under the system menu

I installed CentOS 8 with Gnome 3.28.2 and I noticed that the "switch user" menu item disappeared from under the system menu of Gnome classic (Both X11 & Wayland). I checked google and this problem seems to have a history going back several releases of Gnome. Unfortunately, I never found a... (1 Reply)
Discussion started by: bodisha
1 Replies

2. Shell Programming and Scripting

Execute function as soon as input is provided in menu drive shell script

Hi All, I have a menu driven scripts. As you know while running the script we have to input the option such as 1,2, and 3 to execute function accordingly. but after selecting the input we have to press Enter. My requirement is to execute function as soon as we press the option. Is there... (5 Replies)
Discussion started by: kiran_j
5 Replies

3. Shell Programming and Scripting

Special case to skip function in bash menu

In the bash menu below if the variant that is inputted is in the format NM_004004.3:c.274G>T the below works perfectly. My question is if the variant inputted isNM_004004.3:-c.274G>T or NM_004004.3:+c.274G>T then the code as is will throw an error due to a biological issue. Is it possible to to... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

Need help in create menu with 3 sub menu using the case command

hi all i am a newbie to this is there any examples on creating a main menu with 3 sub menu main menu -> option a , b and c a menu -> option 1 ,2 and 3 b menu -> option 1 ,2 c menu -> option 1 ,2 i am getting headache as my code kept getting unexpected EOF ---------- Post... (0 Replies)
Discussion started by: chercm
0 Replies

5. Shell Programming and Scripting

Menu with sub-menu options

Hi! I have created on script which is working fine with menu options and with a sub-menu. I want to enhance it by using sub-options under menu options. Like. option 1) will give the list only. option 1.1) should give the option to user to choose one file, whose content user wanna see. ... (3 Replies)
Discussion started by: sukhdip
3 Replies

6. Shell Programming and Scripting

KSH- perform a function if user selects option from menu

Hi, I have a script that copies a file from one directory to another and compiles it. What I have now is a menu that calls functions and each function compiles the file. I want to have a function that compiles the file at the end of the script if the user selects options 1-3 in the menu, but... (0 Replies)
Discussion started by: amitlib
0 Replies

7. Shell Programming and Scripting

Menu in Menu script issue

Problem: I am trying to create a menu in a menu script and I am running into an issue with the calculator portion of the script. I am first presented with the ==Options Menu== which all 5 options working correctly. Now comes the fun part. I select option 1 which takes me to my ==Calculator... (1 Reply)
Discussion started by: iDdraig
1 Replies

8. Shell Programming and Scripting

My menu function [new code -revised]

Below is my menu options code. It allows user to pick one of two option. I can get the first option to work, because if it is selected it just "breaks" and carries on with the script. What i want to know is if a user selects option two how do i get that option to ignore all the other script and... (3 Replies)
Discussion started by: amatuer_lee_3
3 Replies

9. UNIX for Dummies Questions & Answers

Menu function stuck in a loop?

I having problem when I call this cleanupmenu function within a script. It continuously loops and goes to selection * That wasn't a valid selection. I have to kill it everytime to stop it. What am I doing wrong. I use this function menu in several other scripts and I don't have this problem at... (2 Replies)
Discussion started by: darthur
2 Replies
Login or Register to Ask a Question