BASH - dynamic menu with nth options for user choice
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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
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
}
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.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.