Go Back   The macosxhints Forums > OS X Help Requests > UNIX - Newcomers



Reply
 
Thread Tools Rating: Thread Rating: 4 votes, 5.00 average. Display Modes
Old 01-07-2009, 09:28 AM   #1
roparzhhemon
Triple-A Player
 
Join Date: Jul 2006
Posts: 106
How do you use the boolean operator && in Bash ?

Hello all,

I have a Bash script that must execute certain things on all
directories but two, called "Breman" and "Scilab/EPS", so at some point in my script I have a test like this :

Code:
   if  [ ( $ff != "Breman" ) && ( $ff != "Scilab/EPS" ) ]; 
             (...)
   fi
With this code, bash -n yields the message
Code:
clean_latex.bash_script: line 11: syntax error near unexpected token `$ff'
clean_latex.bash_script: line 11: `             if [ ( $ff != "Breman" ) && ( $ff != "Scilab/EPS" ) ]; then'
At http://www.linuxtopia.org/online_boo...ect_07_02.html, section 7.2.3, I read that "angular brackets" should not be used and imitating the example in this section I replaced my code with

Code:
   if (( ($ff != "Breman") && ($ff != "Scilab/EPS") )); then
             (...)
   fi
This time bash -n does not say anything but when I actually use the script
I get the error message

Code:
  /Users/ewan/Documents/Bash_scripts/clean_latex.bash_script: line 10: ((: (Boutin != Breman) && (Boutin != Scilab/EPS) : division by 0 (error token is ") ")
/Users/ewan/Documents/Bash_scripts/clean_latex.bash_script: line 10: ((: (Breman != Breman) && (Breman != Scilab/EPS) : division by 0 (error token is ") ")
/Users/ewan/Documents/Bash_scripts/clean_latex.bash_script: line 10: ((: (Difaziadennou != Breman) && (Difaziadennou != Scilab/EPS) : division by 0 (error token is ") ")
/Users/ewan/Documents/Bash_scripts/clean_latex.bash_script: line 10: ((: (Labour_Er_Ger != Breman) && (Labour_Er_Ger != Scilab/EPS) : division by 0 (error token is ") ")
So now I'm quite confused ...
Can anyone help me on this ?

TIA,

Ewan
roparzhhemon is offline   Reply With Quote
Old 01-07-2009, 11:31 AM   #2
nimrod
Registered User
 
Join Date: Jan 2009
Posts: 1
Talking The && is not for the IF

In an IF you should use "-a" for and..
The && is not for IF, I tend to just use it on the command line

# [[ 0 -eq 0 ]] && echo hello
hello
# [[ 1 -eq 0 ]] && echo hello
#
# [[ 1 -eq 0 ]] || echo hello
hello
nimrod is offline   Reply With Quote
Old 01-07-2009, 11:42 AM   #3
Mikey-San
Hall of Famer
 
Join Date: Jan 2002
Posts: 3,541
Quote:
Originally Posted by nimrod
In an IF you should use "-a" for and..
The && is not for IF, I tend to just use it on the command line

# [[ 0 -eq 0 ]] && echo hello
hello
# [[ 1 -eq 0 ]] && echo hello
#
# [[ 1 -eq 0 ]] || echo hello
hello

The logical && operator works just fine in bash:

Code:
if [[ $x == 0 ]] && [[ $y == 0 ]] ; then
echo "hurf"
fi
__________________
COMPUTER TYPE
SOME SPECIFICATIONS I COPIED FROM THE BOX
STUFF I INSTALLED ALL BY MYSELF
"WITTY QUOTE"
Mikey-San is offline   Reply With Quote
Old 01-07-2009, 11:51 AM   #4
roparzhhemon
Triple-A Player
 
Join Date: Jul 2006
Posts: 106
Quote:
Originally Posted by nimrod
The && is not for IF, I tend to just use it on the command line

hello

This is certainly a very sensible thing to do, and I'll follow
your example ... In fact, can "-a" always be used for logical AND
or are there instances where you have to use that horribly
confusing "&&" ?


Ewan
roparzhhemon is offline   Reply With Quote
Old 01-07-2009, 12:20 PM   #5
Mikey-San
Hall of Famer
 
Join Date: Jan 2002
Posts: 3,541
Quote:
Originally Posted by roparzhhemon
This is certainly a very sensible thing to do, and I'll follow
your example ... In fact, can "-a" always be used for logical AND
or are there instances where you have to use that horribly
confusing "&&" ?


Ewan

Just remember that the assertion that && is "not for IF" is wrong. Use it if you like, or feel free use the alternate operator. (I use && because it's in every language I use except AppleScript, therefore it's more natural for me.)
__________________
COMPUTER TYPE
SOME SPECIFICATIONS I COPIED FROM THE BOX
STUFF I INSTALLED ALL BY MYSELF
"WITTY QUOTE"
Mikey-San is offline   Reply With Quote
Old 01-07-2009, 03:47 PM   #6
Hal Itosis
Hall of Famer
 
Join Date: Apr 2002
Posts: 3,315
Using -a is less efficient than && because it always runs though both tests
even if the first one returns a value that will cause the whole thing to fail.

if [ cat = dog -a 1 -eq 1 ]; then echo yes; else echo no; fi
if [ cat = dog ] && [ 1 -eq 1 ]; then echo yes; else echo no; fi

Both of those echo no... but the first one tests if 1 -eq -1 despite the fact that
cat wasn't equal to dog. Once the second one finds failure with the cat = dog
test, it doesn't bother testing any further... since it's smart enough to tell that
the outcome doesn't depend on anything that happens after.

--

The problem with post #1 is all those parens.
I don't know what all the variables refer to...
but this would probably work:

if [[ $ff != Breman && $ff != Scilab/EPS ]]
then

(the double brackets are cool because we can skip most of the "quoting" BS)

Last edited by Hal Itosis; 01-07-2009 at 03:55 PM.
Hal Itosis is offline   Reply With Quote
Old 01-07-2009, 09:38 PM   #7
roparzhhemon
Triple-A Player
 
Join Date: Jul 2006
Posts: 106
Thanks for your comprehensive answer Hal ...

Quote:
Originally Posted by Hal Itosis
--

The problem with post #1 is all those parens.

Quote:
Originally Posted by Hal Itosis
--

(the double brackets are cool because we can skip most of the "quoting" BS)[/size]

Yep ... I tend to over-parenthesize everything because it makes me feel "safer".

Ewan
roparzhhemon is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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



All times are GMT -5. The time now is 02:32 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.