Menu using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Menu using shell script
# 1  
Old 02-06-2012
Menu using shell script

Hi,

I need to have a shell script for the below need.

1. Menu with one heading and 4 options.
2. the heading and 4 options are taken from a file.
File entry
======
Heading1|option1|option2|option3|option4|
Heading2|option1|option2|option3|option4|
3. the user entries must be captured in a file. say, response
4. the user must get the next menu with heading 2, followed by 4 options.

Its a kind of survey.
I have it in python, but then wanted it in shell script.
Can anyone help me out here!

Thanks in advance.
# 2  
Old 02-06-2012
What have you tried so far?
Have you search through the forums?
# 3  
Old 02-06-2012
Well I thought this dynamic menu was going to be a simple example of looping through the input file that holds the menu structure, parsing the menu options line, then using the ksh select construct to prompt, but my select construct is not prompting and instead seems to be taking the input from the while loop read. Can anyone spot why this is happening? ksh93 on Solaris by the way. I thought maybe it was IFS but even if I reset it inside the main loop the same happens.

I recently posted an example of nested menus: https://www.unix.com/shell-programmin...#post302595280 but this version differs with reading from the input file.

The menu input file called dynmenu.dat:
Code:
Heading1|h1_option1|h1_option2|h1_option3|h1_option4|
Heading2|h2_option1|h2_option2|h2_option3|h2_option4|
Heading3|h3_option1|h3_option2|h3_option3|h3_option4|

The script called dynmenu:
Code:
#!/usr/dt/bin/dtksh

typeset -r -x DYNMENUFILE=${0}.dat  # Menu file
typeset -r MAINPROMPT="Enter your selection: "
typeset -x PS3=$MAINPROMPT  # PS3 is the prompt for the select construct.
typeset -r RESPONSEFILE="${0}_${USER}.out"

> $RESPONSEFILE  # Create or clear the response file.

# Read lines from the menu file.
while IFS="|" read heading option1_in option2_in option3_in option4_in the_rest
do
  #  Loop until a correct answer is received.
  while :
  do
#    clear
    print "\n$heading\n"
    select option in $option1_in $option2_in $option3_in $option4_in
    do
      case $REPLY in   # REPLY is set by the select construct,
                       # and is the number of the selection.
        1) ;&  # Fall through
        2) ;&  # Fall through
        3) ;&  # Fall through
        4) print "${heading}: $option" >> $RESPONSEFILE
           break  # Break out 2 levels (get the next menu line)
           ;;
        *) # Always code for the unexpected.
           print "\nUnknown option [${REPLY}]"
           sleep 2
           break # redisplay the current menu
           ;;
      esac
    done  # End select
  done  # End while true
done < $DYNMENUFILE

exit 0

I get the menu for line 1 as expected, but immediately get the unknown option error with the reply being the entire line 2 from the file, etc.
Output:
Code:
$ dynmenu

Heading1

1) h1_option1
2) h1_option2
3) h1_option3
4) h1_option4

Unknown option [Heading2|h2_option1|h2_option2|h2_option3|h2_option4|]

Heading1

1) h1_option1
2) h1_option2
3) h1_option3
4) h1_option4

Unknown option [Heading3|h3_option1|h3_option2|h3_option3|h3_option4|]

Heading1

1) h1_option1
2) h1_option2
3) h1_option3
4) h1_option4

Heading1

1) h1_option1
2) h1_option2
3) h1_option3
4) h1_option4

Heading1

1) h1_option1
2) h1_option2
3) h1_option3
4) h1_option4

(Repeats until CTRL-C)

This User Gave Thanks to gary_w For This Post:
# 4  
Old 02-06-2012
You redirect stdin to read the file. 'select' reads an option from stdin. So 'select' ends up reading from the file.. Open it into another fd, and read from that.

I'm not sure if it's -u5 or -u 5.

Code:
exec 5<$DYNMENUFILE

while IFS="|" read -u5 option1_in option2_in option3_in option4_in the_rest
do
...
done

exec 5<&-

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 02-06-2012
DOH! Here's the corrected example then which works. Thanks Corona688!
Code:
#!/usr/dt/bin/dtksh

typeset -r -x DYNMENUFILE=${0}.dat  # Menu file
typeset -r MAINPROMPT="Enter your selection: "
typeset -x PS3=$MAINPROMPT  # PS3 is the prompt for the select construct.
typeset -r RESPONSEFILE="${0}_${USER}.out"

> $RESPONSEFILE  # Create or clear the response file.

# Create file desriptor 5 as input from the menu file, as the select construct needs to 
# read from stdin too.
exec 5<$DYNMENUFILE

# Read lines from the file descriptor associated with the menu file.
while IFS="|" read -u5 heading option1_in option2_in option3_in option4_in the_rest
do
  #  Loop until a valid option is entered.
  while :
  do
   clear
    print "\n$heading\n"
    select option in $option1_in $option2_in $option3_in $option4_in
    do
      case $REPLY in   # REPLY is set by the select construct,
                       # and is the number of the selection.
        1) ;&  # Fall through
        2) ;&  # Fall through
        3) ;&  # Fall through
        4) print "${heading}: $option" >> $RESPONSEFILE
           print "\nYou selected: ${heading}: $option"
           sleep 2
           break 2  # Break out 2 levels (get the next menu line)
           ;;
        *) # Always code for the unexpected.
           print "\nUnknown option [${REPLY}]"
           sleep 2
           break # redisplay the current menu
           ;;
      esac
    done  # End select
  done  # End while true
done  # End reading menu items

# Close fd 5.
exec 5<&-

exit 0


Last edited by gary_w; 02-06-2012 at 05:00 PM..
# 6  
Old 02-06-2012
Instead of having 4 different cases with fall-through -- an option I've never seen in a shell before -- how about one case for all 4?

Code:
[1-4]) ... ;;

# 7  
Old 02-06-2012
Sure, that's just my preference, I usually set it up like this for future changes where someone behind me (a less experienced developer perhaps) may need to add selection-specific code. If so, the framework if you will is ready for it.

I recall that is a habit I picked up when I coded switch statements in C years ago. Maybe it was a standard where I worked or something.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to include menu based options in Shell script?

Hi Friends, I have a menu based tool which requires input/option to proceed further. How to make a shell script ? eg: menu looks like Get_data.sh to continue (y/n) : Here I need to key in "y" to proceed. I want to prepare a script which should consider option y. (5 Replies)
Discussion started by: suresh3566
5 Replies

2. Shell Programming and Scripting

Automate the menu options using shell script

I have a menu option which will look as follows Select a menu option 1.change password 2.login as root user 3.show system version 4.quit Select> 1 please enter the new password: unix reenter the new password: unix press any key to enter (then displays again the menu options to enter the... (4 Replies)
Discussion started by: shivakumar6g
4 Replies

3. Shell Programming and Scripting

Menu shell script help

Hi All, I have written a shell script that show menu driven option. My requirement is that in the menu driven option i want to select multiple choice. i.e if i want to select 1 or 1,2 or 1,2,3 or 2,3 etc .... Can some one help me in that My script. while true; do echo " " ... (8 Replies)
Discussion started by: Nawrajesh
8 Replies

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

5. Shell Programming and Scripting

Shell script menu problem

I have tried searching the forum but i haven't found a solution for this. I have a shell script that presents the users with menus. The menus branch out to sub menus. It is all hunky dory as long as i traverse forward. But if i am in a sub menu and return to the previous menu and choose any... (11 Replies)
Discussion started by: goddevil
11 Replies

6. Shell Programming and Scripting

Shell script menu

hi guys, how would you do the following? I have a menu with 5 options in my shell script: 1. Run function 1 against files 2. Run function 2 against files 3. Run function 3 against files 4. Run function 4 against files 5. Run function 5 against files I'd like to be able to run multiple... (10 Replies)
Discussion started by: rich@ardz
10 Replies

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

8. Shell Programming and Scripting

shell script to alter grub menu.lst

Hi folks, I have a dual-boot Ubuntu/Windows machine and I wanted to create a script to change the menu.lst file so it will change the default boot partition (this is so I can reload the machine remotely and allow it to boot to the Windows partition). Today I have to sudo cp a template file I... (1 Reply)
Discussion started by: ppucci
1 Replies

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

10. UNIX for Dummies Questions & Answers

Changing korn shell script text Menu colors?

Is it possible to change the color of text in a korn shell script Menu? I can change the color of session text through my telnet client but I want to be able to change color text in the Korn shell menu to highlight certain items. (6 Replies)
Discussion started by: darthur
6 Replies
Login or Register to Ask a Question