LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-12-2011, 08:52 PM   #1
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Rep: Reputation: 37
Bash Scripts - need help with multiple conditions in if statements


Hi all,

Would anybody like to help me with comparing multiple items or conditions using 'if' statements?

I want to do something if one or more conditions is true, for example:

If a = 1 or b = 1.

I've tried:

Code:
if [ "$META1" = "$ARRAY" || "$META2" = "$ARRAY" ] ;
But it doesn't work, results with:

Quote:
[: missing `]'
[: too many arguments
I've also tried:

Code:
if [ "$META1" = "$ARRAY" || [ "$META2" = "$ARRAY" ] ] ;
and

Code:
if (( "$META1" = "$ARRAY" || (( "$META2" = "$ARRAY" )) )) ;
None of those methods work for me. Can anyone help with correct way to do this?
 
Old 03-12-2011, 09:00 PM   #2
frankbell
Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Mageia, Mint
Posts: 7,749

Rep: Reputation: 1462Reputation: 1462Reputation: 1462Reputation: 1462Reputation: 1462Reputation: 1462Reputation: 1462Reputation: 1462Reputation: 1462Reputation: 1462
I don't know the answer, but I bet this link does:

http://www.tldp.org/LDP/Bash-Beginne...ers-Guide.html
 
1 members found this post helpful.
Old 03-12-2011, 09:01 PM   #3
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Wait, is it:

Code:
if [ "$META1" = "$ARRAY" ] || [ "$META2" = "$ARRAY" ] ;
 
Old 03-12-2011, 09:14 PM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Thumbs up

Quote:
Originally Posted by greengrocer View Post
Wait, is it:

Code:
if [ "$META1" = "$ARRAY" ] || [ "$META2" = "$ARRAY" ] ;
You are much closer to the answer now! This should be enough to help you over the finish line:

http://mywiki.wooledge.org/BashPitfa...22_.3D_foo_.5D
 
1 members found this post helpful.
Old 03-13-2011, 12:50 AM   #5
Ashkan_s
Member
 
Registered: Jul 2008
Distribution: Fedora
Posts: 77

Rep: Reputation: 22
Take a look at test man page
Code:
man test
 
Old 03-13-2011, 01:03 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
The bash [[..]] extended test command is more suitable for this purpose. It allows the use of the && and || conditional operators inside them, among other things.
Code:
if [[ "$META1" = "$ARRAY" || "$META2" = "$ARRAY" ]] ;
 
1 members found this post helpful.
  


Reply

Tags
bash, conditional


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
BASH script "if then" with multiple conditions dohpaz Programming 8 05-24-2012 04:31 PM
need answers regarding case statements in bash scripts jahobjafwar Linux - Newbie 4 09-04-2010 09:46 PM
BASH How while loops handle multiple if statements? DevonB Linux - Newbie 3 12-28-2009 06:13 AM
Bash: Problems testing multiple statements with logical and (-a or &&) AncientPC Programming 4 04-14-2008 12:16 AM


All times are GMT -5. The time now is 11:23 PM.

Main Menu
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