Fedora Linux Support Community & Resources Center
  #1  
Old 20th September 2012, 05:03 PM
cyborg's Avatar
cyborg Offline
Registered User
 
Join Date: Apr 2004
Location: Brackley, England
Age: 22
Posts: 180
linuxfirefox
If loop in bash with multiple conditions

In python this would be easy:

Code:
if test_var in ["a","b","c"]: print "Yes"
...but is there a way to do the same in bash? Something like:

Code:
if [ "$TEST" == "a" || "b" || "c"  ]; then echo "Yes"; fi
...but obviously, that doesn't work, you'd have to do:

Code:
if [ "$TEST" == "a" ] || [ "$TEST" == "b" ] || [ "$TEST" == "c" ]; then echo "Yes"; fi
__________________
[joe@host]
Reply With Quote
  #2  
Old 20th September 2012, 05:11 PM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,097
linuxfirefox
Re: If loop in bash with multiple conditions

It is simple:
Code:
case "$TEST" in
"a" | "b" | "c") echo "Yes";;
*) echo nope
esac
Reply With Quote
  #3  
Old 20th September 2012, 05:25 PM
cyborg's Avatar
cyborg Offline
Registered User
 
Join Date: Apr 2004
Location: Brackley, England
Age: 22
Posts: 180
linuxfirefox
Re: If loop in bash with multiple conditions

Ah, nice. Spot on. Thanks. I miss OO so much when I need to write a script.
__________________
[joe@host]
Reply With Quote
  #4  
Old 21st September 2012, 04:44 AM
RupertPupkin's Avatar
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 4,616
linuxfedorafirefox
Re: If loop in bash with multiple conditions

Quote:
Originally Posted by cyborg View Post
is there a way to do the same in bash? Something like:

Code:
if [ "$TEST" == "a" || "b" || "c"  ]; then echo "Yes"; fi
Yes, there is:
Code:
if [[ $TEST =~ ^(a|b|c)$ ]]; then echo "Yes"; fi
__________________
OS: Fedora 18 x86_64 | CPU: AMD64 3700+ 2.2GHz | RAM: 2GB PC3200 DDR | Disk: 160GB PATA | Video: ATI Radeon 7500 AGP 64MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Realtek 8110SC
Reply With Quote
  #5  
Old 21st September 2012, 08:40 AM
cyborg's Avatar
cyborg Offline
Registered User
 
Join Date: Apr 2004
Location: Brackley, England
Age: 22
Posts: 180
linuxfirefox
Re: If loop in bash with multiple conditions

Quote:
Originally Posted by RupertPupkin View Post
Yes, there is:
Code:
if [[ $TEST =~ ^(a|b|c)$ ]]; then echo "Yes"; fi
Magic! I thought I'd seen it somewhere before.
__________________
[joe@host]
Reply With Quote
Reply

Tags
bash, conditions, loop, multiple

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A bash loop question glennzo Programming & Packaging 4 29th April 2012 12:47 PM
for loop bash shell programming Turtel Programming & Packaging 5 10th June 2011 10:25 AM
[SOLVED] [Bash]use eg runuser for multiple commands awakendragon Programming & Packaging 4 14th July 2010 09:48 AM
Bash: While loop will not end! _Chipwiz_Ben_ Using Fedora 4 10th January 2010 02:40 PM
Use bash to call c++ program in a loop daviddoria Using Fedora 1 6th April 2009 07:01 PM


Current GMT-time: 12:47 (Monday, 20-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat