LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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-01-2007, 06:55 PM   #1
dohpaz
Member
 
Registered: Feb 2006
Location: Edmonton
Distribution: Slackware 12.2, Ubuntu
Posts: 117

Rep: Reputation: 16
BASH script "if then" with multiple conditions


I am having difficulty figuring out how to use an if/then with multiple
conditions. What I have is:


Code:
if [ $count -gt 0 ] ; then

     ...do some stuff
fi
I want to change that to something like

Code:
if [ $count -gt 0 && $someVar != $var] ; then

     ...do some stuff
fi
This doesn't work.
Can someone show me how to do this in a bash script?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 05-01-2007, 07:05 PM   #2
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Rep: Reputation: 52
Code:
if [ $count -gt 0 ] && [ $somevar != $var ]; then
...do something
fi
-twantrd
 
2 members found this post helpful.
Old 05-01-2007, 07:13 PM   #3
dohpaz
Member
 
Registered: Feb 2006
Location: Edmonton
Distribution: Slackware 12.2, Ubuntu
Posts: 117

Original Poster
Rep: Reputation: 16
Twantrd thank you.
 
Old 05-02-2007, 12:51 AM   #4
Zention
Member
 
Registered: Mar 2007
Posts: 119

Rep: Reputation: 16
Now, this is why [[ ]] is the preferred form to get use to

if [[ $count -gt 0 && $someVar != $var ]]
then

...

fi

though of course the 2ndPoster is also correct.
 
2 members found this post helpful.
Old 02-19-2012, 05:32 AM   #5
OldManRiver
LQ Newbie
 
Registered: Aug 2004
Posts: 24

Rep: Reputation: 5
Help Link

All,

According to Table 7-2 in the help at:

http://tldp.org/LDP/Bash-Beginners-G...ect_07_01.html

the correct syntax is:

AND:
[ EXPR1 -a EXPR2 ]

OR
[ EXPR1 -o EXPR2 ]

Cheers!

OMR
 
Old 05-23-2012, 10:05 AM   #6
battler
LQ Newbie
 
Registered: Apr 2012
Posts: 23

Rep: Reputation: Disabled
The -a (&&) statement works great for two conditions. However I have four conditions how do I do this?

if [$statement1 = "state1" -a $statement2 = "state2" -a $statement3 = "state3" -a $statement4 = "state4"]

This doesnt work, only the first two conditions are being matched. How can I make this work?
 
Old 05-23-2012, 10:38 AM   #7
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,935
Blog Entries: 1

Rep: Reputation: 188Reputation: 188
Quote:
Originally Posted by dohpaz View Post
I am having difficulty figuring out how to use an if/then with multiple
conditions. What I have is:


Code:
if [ $count -gt 0 ] ; then

     ...do some stuff
fi
I want to change that to something like

Code:
if [ $count -gt 0 && $someVar != $var] ; then

     ...do some stuff
fi
This doesn't work.
Can someone show me how to do this in a bash script?
You can use the "-a" (and) or the -o (or) for this...

Code:
if [ $count -gt 0 -a $someVar != $var] ; then

     ...do some stuff
fi
--C
 
Old 05-23-2012, 11:29 AM   #8
battler
LQ Newbie
 
Registered: Apr 2012
Posts: 23

Rep: Reputation: Disabled
custangro, thank you for your reply but this doesnt work with multiple conditions at least not if conditions > 2 .

Right now I use:
Quote:
if ( [[ $VAK = "NOS" ]] && [[ $TYPE = "LES" ]] && [[ $ONDERWERP = "BEVEILIGING" ]] && [[ $ACTIE = "UITVOEREN" ]] ); then
echo "UITVOEREN NOS LES firewall"
This works.

Last edited by battler; 05-23-2012 at 02:00 PM.
 
Old 05-24-2012, 04:31 PM   #9
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
battler, please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

It's also generally better to not reopen old threads unless you have something specific to add to that discussion. Otherwise, open up a new thread for your questions, and link back to the other if appropriate.


I don't like the idea of using so many conditions in a single test anyway. It would be more robust, readable, easily debuggable, and likely more efficient, to break it up into multiple nested tests instead.

Code:
if [[ $VAK = "NOS" && $TYPE = "LES" ]]; then

	if [[ $ONDERWERP = "BEVEILIGING" && $ACTIE = "UITVOEREN" ]]; then

		echo "UITVOEREN NOS LES firewall" 

	fi

fi
Of course, too many nested levels can be unwieldy as well, so two conditions per test seems like a good balance to me.


And as per the previous comments, when using bash or ksh, it's recommended to use ((..)) for numerical tests, and [[..]] for string/file tests and complex expressions. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression


Finally, depending on your exact requirements, it might also be a good idea to look into using case statements, either in combination with, or instead of, if tests.

Last edited by David the H.; 05-24-2012 at 04:37 PM. Reason: small addendum and fixes
 
2 members found this post helpful.
  


Reply


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: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 6 03-21-2013 10:35 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
shell script - while loop with multiple conditions ronsha Programming 13 12-10-2005 04:08 PM
searching "TIC TAC TOE" bash script LV-chronos Linux - Newbie 5 05-29-2005 02:20 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM


All times are GMT -5. The time now is 01:13 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