LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 05-07-2012, 09:16 PM   #1
dth4h
Member
 
Registered: Mar 2009
Posts: 51

Rep: Reputation: 10
Bash question


\/ FORGET THIS, read my second post \/

Code:
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?

Last edited by dth4h; 05-07-2012 at 10:38 PM.
 
Old 05-07-2012, 10:21 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,004
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
But you're accepting "e", "y", and "remove", so your text explanation
doesn't match your code ...
 
Old 05-07-2012, 10:31 PM   #3
dth4h
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 10
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.
 
Old 05-07-2012, 10:53 PM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,236

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
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
 
1 members found this post helpful.
Old 05-07-2012, 11:15 PM   #5
dth4h
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by jlinkels View Post
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.

Anyway, thanks again jlinkels.
 
Old 05-08-2012, 12:05 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
A case statement is more natural and easily allows accepting Y as equivalent to y etc:
Code:
case $answer in
    y | Y | e | E | remove )
        ;;
    * )
        <commands to be run when answer is invalid>
esac
 
1 members found this post helpful.
Old 05-08-2012, 12:15 AM   #7
dth4h
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by catkin View Post
A case statement is more natural and easily allows accepting Y as equivalent to y etc:
Code:
case $answer in
    y | Y | e | E | remove )
        ;;
    * )
        <commands to be run when answer is invalid>
esac
Thanks catkin, that's even better!
 
Old 05-08-2012, 12:21 AM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,004
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
And a third version ... ;D

Code:
regex="y|e|remove"
answer="y"
if [[ $answer =~ $regex ]]; then echo "Known"; fi
Known
answer="blub"
if [[ $answer =~ $regex ]]; then echo "Known"; fi

Cheers,
Tink
 
1 members found this post helpful.
Old 05-08-2012, 12:25 AM   #9
dth4h
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by Tinkster View Post
And a third version ... ;D

Code:
regex="y|e|remove"
answer="y"
if [[ $answer =~ $regex ]]; then echo "Known"; fi
Known
answer="blub"
if [[ $answer =~ $regex ]]; then echo "Known"; fi

Cheers,
Tink
I think this is my most favorite thread ever! Thanks everyone!
 
Old 05-08-2012, 01:02 AM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,004
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Wink

Quote:
Originally Posted by dth4h View Post
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 ;}
 
Old 05-08-2012, 01:15 AM   #11
dth4h
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by Tinkster View Post
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
 
Old 05-08-2012, 01:18 AM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,004
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by dth4h View Post
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
Easy fix
Code:
regex='^y$|^e$|^remove$'
;0)
 
1 members found this post helpful.
Old 05-08-2012, 01:19 AM   #13
dth4h
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by Tinkster View Post
Easy fix
Code:
regex='^y$|^e$|^remove$'
;0)
Ahhh, cool. Thanks
 
Old 05-08-2012, 04:13 AM   #14
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
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
 
2 members found this post helpful.
Old 05-08-2012, 07:41 AM   #15
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Nice, grail, nice Robust and elegant
 
  


Reply

Tags
bash, syntax


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Using Bash for yes/no question nkitmitto Programming 12 07-11-2010 08:18 PM
Bash question armandino101 Linux - General 6 04-24-2007 01:18 PM
Bash question lnxduck Programming 3 09-30-2005 09:50 AM
bash question shanenin Linux - Software 3 02-14-2004 07:10 PM
Bash question J_Szucs Programming 9 01-13-2003 04:19 AM


All times are GMT -5. The time now is 12:20 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration