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.
Maybe there's another way to select an option without using the select syntax, and I'm looking for a bash solution, I'd prefer not to use xenity or dialog.
Just for your inspiration. Once upon a time I wrote this script that selects from options. I ended up not using select, but there're several implementations in the source including two with select.
I realise the length issue was the original question, but you are aware that your case statement is not going to work, or at least definitely not like you think??
I realise the length issue was the original question, but you are aware that your case statement is not going to work, or at least definitely not like you think??
You're correct, as written it doesn't work, but the length issue was attended to.
Here's one that works, or at least how I want it to:
Code:
#!/bin/bash
# Bash Menu Script Example
## A few aliases are also available for ABR mode:
## phone => 16kbps/mono phon+/lw/mw-eu/sw => 24kbps/mono
## mw-us => 40kbps/mono voice => 56kbps/mono
## fm/radio/tape => 112kbps hifi => 160kbps
## cd => 192kbps studio => 256kbps
COLUMNS=20
PS3='Please enter your choice: '
A="phone 16kbps/mono";B="sw 24kbps/mono";C="mw-us 40kbps/mono";D="voice 56kbps/mono";E="Quit"
options=("$A" "$B" "$C" "$D" "$E")
select opt in "${options[@]}"
do
case $opt in
"$A") break ;;
"$B") break ;;
"$C") break ;;
"$D") break ;;
"Quit") echo "quitting";exit ;;
*) echo "invalid option $REPLY" ;;
esac
done
echo "====================================="
echo "ABR preset is $opt - ${opt%% *}"
echo "Prompt for filename, title, artist etc"
echo "====================================="
echo "Now this will continue with the processing"
echo "part of the script......."
echo "====== Finished Okay ================"
exit
and a few terminal grabs to show it working:
Code:
charlie@charlie-machine:~/000TEST$ ./Test.sh
1) phone 16kbps/mono
2) sw 24kbps/mono
3) mw-us 40kbps/mono
4) voice 56kbps/mono
5) Quit
Please enter your choice: <===== blank entry
1) phone 16kbps/mono
2) sw 24kbps/mono
3) mw-us 40kbps/mono
4) voice 56kbps/mono
5) Quit
Please enter your choice: 6 <===== invalid selection
invalid option 6
Please enter your choice: 4 <===== valid selection
=====================================
ABR preset is voice 56kbps/mono - voice
Prompt for filename, title, artist etc
===================================== <===== processing part with option 4
Now this will continue with the processing
part of the script.......
====== Finished Okay ================
charlie@charlie-machine:~/000TEST$ ./Test.sh
1) phone 16kbps/mono
2) sw 24kbps/mono
3) mw-us 40kbps/mono
4) voice 56kbps/mono
5) Quit
Please enter your choice: 5 <===== quit option
quitting
charlie@charlie-machine:~/000TEST$
Cool, here is what I like to do, if you like some of it steal it, if not it is here for future onlookers
Code:
#!/usr/bin/env bash
COLUMNS=20
PS3="Please enter your choice: "
options=(
"phone 16kbps/mono"
"sw 24kbps/mono"
"mw-us 40kbps/mono"
"voice 56kbps/mono"
)
# Select from array plus adding Quit option so array can be added to easily
select opt in "${options[@]}" Quit
do
# Catching the issue where the user inputs non-numerical entries
[[ "$REPLY" =~ [^0-9] ]] && (( REPLY = $# + 1 ))
# Break on any valid choice as information stored in opt is what is required
if (( REPLY <= ${#options[*]} + 1 ))
then
break
else
echo "ERROR: Invalid choice. Must choose one of the numbers provided."
fi
done
if [[ "$opt" == Quit ]]
then
echo "quitting"
else
# Use heredoc to output large amounts of text
# Hyphen (-) before 'OUT' is impoartant as it allows for formatting in editor to be constant
# and all tabs prior to text in heredoc will be removed
cat <<-OUT
=====================================
ABR preset is $opt - ${opt%% *}
Prompt for filename, title, artist etc
=====================================
Now this will continue with the processing
part of the script.......
====== Finished Okay ================
OUT
fi
Sorry for the weird indenting, my tabs are only 2 spaces in size, but bigger here
Last edited by grail; 04-10-2020 at 10:51 PM.
Reason: Comments added
Cool, here is what I like to do, if you like some of it steal it, if not it is here for future onlookers
Code:
#!/usr/bin/env bash
COLUMNS=20
PS3="Please enter your choice: "
options=(
"phone 16kbps/mono"
"sw 24kbps/mono"
"mw-us 40kbps/mono"
"voice 56kbps/mono"
)
select opt in "${options[@]}" Quit
do
[[ "$REPLY" =~ [^0-9] ]] && (( REPLY = $# + 1 ))
if (( REPLY <= ${#options[*]} + 1 ))
then
break
else
echo "ERROR: Invalid choice. Must choose one of the numbers provided."
fi
done
if [[ "$opt" == Quit ]]
then
echo "quitting"
else
cat <<-OUT
=====================================
ABR preset is $opt - ${opt%% *}
Prompt for filename, title, artist etc
=====================================
Now this will continue with the processing
part of the script.......
====== Finished Okay ================
OUT
fi
Sorry for the weird indenting, my tabs are only 2 spaces in size, but bigger here
Grail, very nice, very neat, I'm going through it line by line, but for others it may be useful to comment on each line what it's doing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.