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.
while [ $continue = no ];do
read -p "Some question" answer
if [ "$answer" != "1" -a "$answer" != "2" -a "$answer" != "3" -a "$answer" != "4" -a "$answer" != "5" -a "$answer" != "6" -a "$answer" != "y" -a "$answer" != "e" -a "$answer" != "remove" ];then
echo "Option not recognised"
continue=no
fi
The third line is the only one that is really relevant. What is a simpler way of writing the third line? Basically instead of it checking for all those numbers, I just want to make sure that it is a number, and that it isn't more then 99.
And for the other options, my Dad said he remembered that he used to do something like this:
if [ "$answer" != [y|e|remove] ];then
but he doesn't remember the exact syntax. And I tried that and it didn't work. So do any of you know the right way of doing that or something similar?
Ok forget about everything else. I figured out how to do the number part. I just want to know if there is something similar to this that is right: if [ "$answer" != [y|e|remove] ];then
This put in a sentence "If the answer is not y, e or remove then"
My long current way of doing this that I want to shorten (because if I have like 20 options then this line could get really long):
Code:
while [ $continue = no ];do
read -p "Some question" answer
if [ "$answer" != "y" -a "$answer" != "e" -a "$answer" != "remove" ];then
echo "Option not recognised"
continue=no
fi
NOTE: The options are not important, I just used them as examples.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
This is maybe not very efficient, but it seems elegant:
Code:
correct_answer=0
for test_answer in y n remove spam eggs foo bar
do
if [ $answer == $test_answer ]
then
correct_answer=1
fi
done
if [ $correct_answer -ne 1 ]
then
#do something
fi
Beware, this is not tested code, it might contain errors!
This is maybe not very efficient, but it seems elegant:
Code:
correct_answer=0
for test_answer in y n remove spam eggs foo bar
do
if [ $answer == $test_answer ]
then
correct_answer=1
fi
done
if [ $correct_answer -ne 1 ]
then
#do something
fi
Beware, this is not tested code, it might contain errors!
jlinkels
This works. I never knew there was "for" to use for loops. I thought there was only while. This is so cool. Thanks !
I am still curious though if there is a right way of doing what I posted. I know my Dad used to do something like it and I have seen it a few places on the internet before, but when I wanted to use it I could never find it again.
I think this is my most favorite thread ever! Thanks everyone!
In that case show the participants some love, and either click on the
little scales on the far left of their posts, or click the "helpful"
link within their posts ;}
In that case show the participants some love, and either click on the
little scales on the far left of their posts, or click the "helpful"
link within their posts ;}
Oh, that's cool. I will do that.
One note though. A side effect of your solution is that you can enter a lot of other things that aren't in the variable "regex" and the answer will be accepted. I haven't quite been able to figure out the pattern yet but here are some examples:
re
reeeeeeeeeeeeee
er
yre
One note though. A side effect of your solution is that you can enter a lot of other things that aren't in the variable "regex" and the answer will be accepted. I haven't quite been able to figure out the pattern yet but here are some examples:
re
reeeeeeeeeeeeee
er
yre
Tink Tink Tink .... you must be getting slack in your old age ... lol .. just kidding:
Code:
regex='^(y|e|remove)$'
I would add that to remove the element of case surprise, I would add:
Code:
case ${answer,,} in
# or
if [[ ${answer,,} =~ $regex ]]
Also, you indicated that there are many options, so to help with creating the regex, I would suggest an array:
Code:
elements=(y e remove)
# or if stored in a file with one option per line
#elements=($(< file_with_options))
IFS="|"
regex="^(${elements[*]})$"
unset IFS
if [[ ${answer,,} =~ $regex ]]; then echo "it's here";fi
# or with the case statement
# IFS="|"
# case ${answer,,} in
# "${elements[*]}") echo it has an element;;
# *) echo it does not;;
# esac
# unset IFS
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.