Main Page | Report Page

 

  Linux Forum Index » Linux Miscellaneous Topics 2 » logical OR in bash

Author Message
Yanb
Posted: Wed Feb 06, 2008 4:40 am
 
Hi, a simple question, I'm new to bash (and linux completely:-)

How do I compare more things in bash?

this works fine:
---
echo "Are you sure? Type YES and press ENTER."
read LINE
if [ "$LINE" != "yes" ]; then
exit 1
fi
echo "great"
---

but what if I want to compare to "YES". I tried whatever I know from php or C, but anything like
if [ (("$LINE" != "yes") && ("$LINE" != "YES")) ]; then

does not work. I know I sloudh convert it to upper case for best result, whatever, but I need to know
how to make more conditions in single if :-)

So how to make it right? Wink Thank you in advance.

Yanb
(stands for yet another newbie:-)
 
Clyde.bis
Posted: Wed Feb 06, 2008 6:52 am
 
Yanb ha scritto:

Quote:
I need to know how to make more conditions in single if

The AND operator is done with "-a" in a test statement:

if [ "$LINE" != "yes" -a "$LINE" != "YES" ]; then

Parentheses and "&&" have nothing to do with the classic test.
However, they are accepted with the less classic [[ ]] bash built-in
command:

if [[ "$LINE" != "yes" && "$LINE" != "YES" ]]; then

For more info:

help test or man test
help \[\[
man bash

--
no signature yet
 
Ignoramus28484
Posted: Wed Feb 06, 2008 10:47 am
 
On 2008-02-06, Yanb <Yanb@whatever.invalid> wrote:
Quote:
Hi, a simple question, I'm new to bash (and linux completely:-)

How do I compare more things in bash?

this works fine:
echo "Are you sure? Type YES and press ENTER."
read LINE
if [ "$LINE" != "yes" ]; then
exit 1
fi
echo "great"

but what if I want to compare to "YES". I tried whatever I know from php or C, but anything like
if [ (("$LINE" != "yes") && ("$LINE" != "YES")) ]; then

does not work. I know I sloudh convert it to upper case for best result, whatever, but I need to know
how to make more conditions in single if :-)

So how to make it right? Wink Thank you in advance.

Yanb
(stands for yet another newbie:-)

I do not like the monkey style with [ and ], just use "test", it is
easier to read.

if test "$LINE" = "yes" && test "$LINE" = "YES"; then
echo great
fi
 
Dan Espen
Posted: Wed Feb 06, 2008 11:50 am
 
Yanb <Yanb@whatever.invalid> writes:

Quote:
Hi, a simple question, I'm new to bash (and linux completely:-)

How do I compare more things in bash?

this works fine:
---
echo "Are you sure? Type YES and press ENTER."
read LINE
if [ "$LINE" != "yes" ]; then
exit 1
fi
echo "great"
---

but what if I want to compare to "YES". I tried whatever I know from php or C, but anything like
if [ (("$LINE" != "yes") && ("$LINE" != "YES")) ]; then

does not work. I know I sloudh convert it to upper case for best result, whatever, but I need to know
how to make more conditions in single if :-)

So how to make it right? Wink Thank you in advance.

Shell comparisons are done best with the case statement since it supports
regular expressions.

Example:

case $LINE in
YES | yes)
echo "matched"
;;
* )
echo "no match"
;;
esac

Another possibility:

[yY][eE][sS]
 
Yanb
Posted: Wed Feb 06, 2008 9:21 pm
 
Ignoramus28484 <ignoramus28484@NOSPAM.28484.invalid> wrote in
news:eqSdnZBSI54bVTTanZ2dnUVZ_s7inZ2d@giganews.com:

Quote:
I do not like the monkey style with [ and ], just use "test", it is
easier to read.

if test "$LINE" = "yes" && test "$LINE" = "YES"; then
echo great
fi

It looks really more simple. If this would work, perhaps I'd like it more Wink Unfortunately your way does not
work for me (in cygwin). No matter what $LINE is, the condition is never true Sad But thank you anyway.
 
Yanb
Posted: Wed Feb 06, 2008 9:21 pm
 
Dan Espen <daneNO@MORE.mk.SPAMtelcordia.com> wrote in
news:ic1w7qqe2e.fsf@mk.telcordia.com:

Quote:
Shell comparisons are done best with the case statement since it
supports regular expressions.

Example:

case $LINE in
YES | yes)
echo "matched"
;;
* )
echo "no match"
;;
esac

Another possibility:

[yY][eE][sS]


Thank you, this way will help me further in my script Smile Looks like you have answered what I have not
asked yet, but I'd most probably ask later, if I wouldn't google it out Smile
 
Yanb
Posted: Wed Feb 06, 2008 9:34 pm
 
"Clyde.bis" <spamfilter.virgilio@gmail.com> wrote in news:focojq$lah$1@tdi.cu.mi.it:

Quote:
Yanb ha scritto:

I need to know how to make more conditions in single if

The AND operator is done with "-a" in a test statement:

if [ "$LINE" != "yes" -a "$LINE" != "YES" ]; then

Parentheses and "&&" have nothing to do with the classic test.
However, they are accepted with the less classic [[ ]] bash built-in
command:

if [[ "$LINE" != "yes" && "$LINE" != "YES" ]]; then

For more info:

help test or man test
help \[\[
man bash


Thank you, both ways are working fine, I'll use one. I know I should have RTFM, but the man page for
bash is the longest I have seen so far, without any examples there, so I have rather asked here Wink
 
Yanb
Posted: Thu Feb 07, 2008 12:00 am
 
floyd@apaflo.com (Floyd L. Davidson) wrote in news:87zludw5s3.fld@apaflo.com:

Quote:
[snip]

(Assuming that I haven't added any typos when copying these...
I did pretty much cut and past from a test script to make sure
that I did it right, but there is always another edit or two
that gets done, so...)


Wow, thank you for the EXCELENT examples! Smile Not tested all of them, only those I'm able to
understand (so far:), and seems to be typo-free and working .-)
 
Floyd L. Davidson
Posted: Thu Feb 07, 2008 4:05 am
 
Yanb <Yanb@whatever.invalid> wrote:
Quote:
Ignoramus28484 <ignoramus28484@NOSPAM.28484.invalid> wrote in
news:eqSdnZBSI54bVTTanZ2dnUVZ_s7inZ2d@giganews.com:

I do not like the monkey style with [ and ], just use "test", it is
easier to read.

if test "$LINE" = "yes" && test "$LINE" = "YES"; then
echo great
fi

It looks really more simple. If this would work,
perhaps I'd like it more Wink Unfortunately your way
does not work for me (in cygwin). No matter what $LINE
is, the condition is never true Sad But thank you
anyway.

Typo alert! What he meant to suggest was a little different,
because there is no way that a string can be equal to two
*different* comparisons. Either of these will work:

if test "$LINE" = "yes" || test "$LINE" = "YES"; then
echo "The answer is yes."
else
echo "The answer is no."
fi

if test "$LINE" != "yes" && test "$LINE" != "YES"; then
echo "The answer is no."
else
echo "The answer is yes."
fi

Using the standard [ ... ] convention, either of these will
do:

if [ "$LINE" != "yes" -a "$LINE" != "YES" ] ; then
echo "The answer is no."
else
echo "The answer is yes."
fi

if [ "$LINE" = "yes" -o "$LINE" = "YES" ] ; then
echo "The answer is yes."
else
echo "The answer is no."
fi

As also suggested, the case statement is very useful too:

case "$LINE" in
[Yy][Ee][Ss]) echo "The answer is yes.";;
*) echo "The answer is no.";;
esac

And another interesting variation on that (anything beginning
with a "Y", regardless of case, is "yes"):

case "$LINE" in
[Yy]*) echo "The answer is yes.";;
*) echo "The answer is no.";;
esac

And the same can be done with the original construct too:

if [ "${i:0:1}" != "y" -a "${i:0:1}" != "Y" ] ; then
echo "Tests as no."
else
echo "Tests as yes."
fi

Or

if test "${i:0:1}" = "y" || test "${i:0:1}" = "Y"; then
echo "Tests as yes."
else
echo "Tests as no."
fi


(Assuming that I haven't added any typos when copying these...
I did pretty much cut and past from a test script to make sure
that I did it right, but there is always another edit or two
that gets done, so...)

--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@apaflo.com
 
Ignoramus29783
Posted: Thu Feb 07, 2008 8:19 am
 
On 2008-02-07, Yanb <Yanb@whatever.invalid> wrote:
Quote:
Ignoramus28484 <ignoramus28484@NOSPAM.28484.invalid> wrote in
news:eqSdnZBSI54bVTTanZ2dnUVZ_s7inZ2d@giganews.com:

I do not like the monkey style with [ and ], just use "test", it is
easier to read.

if test "$LINE" = "yes" && test "$LINE" = "YES"; then
echo great
fi

It looks really more simple. If this would work, perhaps I'd like it more Wink Unfortunately your way does not
work for me (in cygwin). No matter what $LINE is, the condition is never true Sad But thank you anyway.

my apology for a typo, the && should be replaced with OR ||
 
Dan Espen
Posted: Thu Feb 07, 2008 10:02 am
 
Yanb <Yanb@whatever.invalid> writes:

Quote:
Dan Espen <daneNO@MORE.mk.SPAMtelcordia.com> wrote in
news:ic1w7qqe2e.fsf@mk.telcordia.com:

Shell comparisons are done best with the case statement since it
supports regular expressions.

Example:

case $LINE in
YES | yes)
echo "matched"
;;
* )
echo "no match"
;;
esac

Another possibility:

[yY][eE][sS]


Thank you, this way will help me further in my script Smile Looks like
you have answered what I have not asked yet, but I'd most probably
ask later, if I wouldn't google it out Smile

One further pointer.

If this script is going to grow beyond 40 lines or so,
you'll be much better off using Perl.

Similar logic in Perl:

if ( "$LINE" =~ /yes/i ) {
print 'entered yes';
}
 
Ignoramus29783
Posted: Thu Feb 07, 2008 10:41 am
 
On 2008-02-07, Dan Espen <daneNO@MORE.mk.SPAMtelcordia.com> wrote:
Quote:
Yanb <Yanb@whatever.invalid> writes:

Dan Espen <daneNO@MORE.mk.SPAMtelcordia.com> wrote in
news:ic1w7qqe2e.fsf@mk.telcordia.com:

Shell comparisons are done best with the case statement since it
supports regular expressions.

Example:

case $LINE in
YES | yes)
echo "matched"
;;
* )
echo "no match"
;;
esac

Another possibility:

[yY][eE][sS]


Thank you, this way will help me further in my script Smile Looks like
you have answered what I have not asked yet, but I'd most probably
ask later, if I wouldn't google it out :-)

One further pointer.

If this script is going to grow beyond 40 lines or so,
you'll be much better off using Perl.

Similar logic in Perl:

if ( "$LINE" =~ /yes/i ) {
print 'entered yes';
}

Absolutely. Using perl opens up a whole new world of automation.

print "Entered yes\n" if $LINE =~ /^yes/i;
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Mon Sep 01, 2014 8:49 am