LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2014, 06:03 AM   #1
klmn
LQ Newbie
 
Registered: Oct 2014
Posts: 3

Rep: Reputation: Disabled
BASH - dynamic menu with nth options for user choice


Hi

Explanation: I can't paste code beacuse there are some confidential data and changing it is to complex.

Description:

Function in my script is getiing output from some commands with grep and awk and creating array with values from output from this commands. This situation can be compared to execution of "ls" in some random catalog and getting different amount of files names and inserting them to an array.

Problem:

I want to limit human operator mistakes and automate some procesess - so I want to create some kind of menu for users to make a choice, but number of options is variable and depends on number of files in catalog, inserted into a araay. My ideda is to display some description and menu like:

Code:
Please choose option below:

1. "Option one" # ${array[${0}]}
2. "Option two" # ${array[${1}]}
...
X. "Option X" # ${array[${X}]}
and after choosing some option - return choosed value to another function in a scrupt. Value should ba numeric (integer) in order to maintain previously used naming convention and script behaviour.

Solution:

I don't expect a strict and working code to copy/paste into my script - I would expect some ideas to check: maybe some example of using "case" or "select" or some other solution that could fit into my problem.

Thx in advance ! :-)

Last edited by klmn; 10-30-2014 at 08:00 AM. Reason: Marking a code in post
 
Old 10-30-2014, 06:38 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
have you checked select already? Is it working for you, or was there some problems with that?
 
Old 10-30-2014, 06:40 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Under the parameters of the example you've shown; there's absolutely nothing wrong with using "echo" to output the list of options to the terminal, next using "read" to get their answer, and finally using if-elif-else statements to determine their answer. And there are better ways to compare, or use compound if statements, or probably just other techniques, what's showing here is just simple.

This is a function from a script I wrote which is used to format a system disk for a target device; the user inserts a compact flash into a reader and invokes the script with the /dev/sd[letter] path of the inserted disk and one or more options, that information gets parsed and this simple function checks whether or not the user wishes to proceed. One can expand this example and in fact use switch-case to parse the answer for a greater number of permutations.
Code:
# Present Options
# Inform the user of the script's actions and
# what it thinks they've chosen to do and then
# give them an option to proceed or cancel.
present_options()
{
    echo ""
    echo ""
    echo "This script prepares a disk with a release."
    echo "Options are to:"
    echo "     - Format and set up the entire disk (use -f)"
    echo "     - Copy the kernel only (use -norfs)"
    echo "     - Copy the kernel and Root File System (RFS) (default)"
    echo ""
    echo "Choices cannot be combined.  Format (-f) option assumes full disk overwrite."
    echo ""
    echo "Options chosen:"
    echo "     Device: $1"
    echo "       Kernel will be copied"
    if [ $FORMAT -eq 1 ]; then
        echo "       Disk will be formatted"
    else
        echo "       Disk will NOT be formatted"
        if [ $RFS -eq 1 ]; then
            echo "       Root File System will be copied"
        else
            echo "       Root File System will NOT be copied"
        fi
    fi
    echo ""
    echo "Do you wish to proceed? (y/[n]) "
    read ANSWER
    if [ -z $ANSWER ]; then
        echo "Aborting(1) . . ."
        echo ""
        exit -1
    elif [ $ANSWER == "Y" ]; then
        echo "Proceeding with disk setup . . ."
        echo ""
        return 0
    elif [ $ANSWER == "y" ]; then
        echo "Proceeding with disk setup . . ."
        echo ""
        return 0
    else
        echo "Aborting . . ."
        echo ""
        exit -1
    fi
}
 
Old 10-30-2014, 07:58 AM   #4
klmn
LQ Newbie
 
Registered: Oct 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thx for reply, rtmistler - but your example is prepared for some defined number of answers/options, in my case I'm trying to force user to use some given option, but number of this options vary and depends on choices made previously in script and environemnt, so I can't define options in advance - I have to extract X number of options from array and then prepare menu for user with 1 choice from X options.
 
Old 10-30-2014, 08:13 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by klmn View Post
Thx for reply, rtmistler - but your example is prepared for some defined number of answers/options, in my case I'm trying to force user to use some given option, but number of this options vary and depends on choices made previously in script and environemnt, so I can't define options in advance - I have to extract X number of options from array and then prepare menu for user with 1 choice from X options.
You can extend that example to use variables in place of exacting values. Echo will output variables from arrays, and you can test against variables.

Just as in your example where you used
Code:
1. "Option one" # ${array[${0}]}
2. "Option two" # ${array[${1}]}
...
X. "Option X" # ${array[${X}]}
You can use the array elements in your echo statements and when you read ANSWER you then compare it against the array elements.

Issues are whether you allow or detect case sensitivity. I don't know about select as pan64 suggested here, but also something to check out. A quick google of that shows that it's a way to parse input.

Write a test script. Create an array, elicit input from stdin (a.k.a. the user) and then write some script to parse that output.

I'm not sure switch-case will be easy to code when you have to use variables. Unless the array is always fixed in size. That would be the case for experimentation.

Another benefit of writing a test example is that it doesn't give out proprietary information because it would be made-up code. Then you could post your attempt and honestly there are a TON of great scripters on the forum who will be extremely helpful in assisting you with refining the script.

Last edited by rtmistler; 10-30-2014 at 10:24 AM.
 
Old 10-30-2014, 10:08 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am curious what is wrong with just standard select? (as queried in first reply)
 
Old 10-30-2014, 10:22 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by grail View Post
I am curious what is wrong with just standard select? (as queried in first reply)
Likely nothing at all, I have zero experience with it in BASH, but did suggest that the OP google about it. I did and it appears to be helpful. Funnily enough, I use select(2) a ton in C, but it's also a totally different function as used in C.

To be honest, I feel the OP needs to make an attempt and post it. They were polite enough to say that they weren't expecting someone to just post a script for them to plug right in; however my point is that they ought to try something and when they get stuck, post some follow-up.
 
Old 10-30-2014, 11:17 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Sorry that you thought that was directed at you rtmistler I had meant for the OP to answer as you were posting a different direction I was happy for him to follow that with you

And yes, I 100% agree that the OP needs to show an example and where they are stuck. The excuse of sensitive data should never preclude you from isolating the snippet needed to be expanded upon.
 
Old 10-31-2014, 06:25 PM   #9
pix9
Member
 
Registered: Jan 2010
Location: Mumbai, India
Distribution: ArchLinux, Fedora 24, Centos 7.0
Posts: 177

Rep: Reputation: 19
Lightbulb

Hello,
From your description I've understood some stuff,I've created an example script hope that might help you, I would suggest create diffrent functions and call them in with case, as given in script below.

Code:
#!/bin/bash

function1(){
CHOICE=NULL
echo "This is subfunction 1"
read -p "please enter your choice" CHOICE

case $CHOICE in
 1)
    echo "1" 
;;
 2)
    echo "2" 
;;
 3) 
    echo "3" 
;;
esac
}


function2(){
CHOICE=NULL
echo "This is sub function 2"
read -p "please enter your choice" CHOICE

case $CHOICE in
 1)
    echo "21" 
;;
 2)
    echo "22" 
;;
 3) 
    echo "23" 
;;
esac
}

OPT=NULL
echo "This is main"
read -p "please enter your" OPT

case $OPT in
  1)
function1
;;
  2)
function2
;;
esac
hope my script would help you in achiving your task.
Forgot to mention These functions can be used to build your menu.

regards

Last edited by pix9; 10-31-2014 at 08:46 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
the other menu choice in debian lxde omran97 Linux - Newbie 1 08-21-2013 07:54 PM
dynamic menu in Vtwm - need help getting it done linuxlicious Linux - Desktop 5 03-03-2013 08:46 AM
why can't I use the "switch User "Options on start menu addie_goodvibes Linux - Newbie 3 03-23-2009 08:43 PM
Boot choice menu is missing after installation of another OS gregorian Linux - Newbie 7 05-16-2006 10:52 PM
adding a menu choice in GDM pHaT tAcO Linux - Software 6 09-19-2003 05:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration