help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] New conditional evaluation with a regex


From: Jesse Molina
Subject: Re: [Help-bash] New conditional evaluation with a regex
Date: Thu, 08 Dec 2011 03:49:26 -0700
User-agent: Mozilla/5.0 (X11; Linux i686; rv:8.0.1) Gecko/20111121 Firefox/8.0.1 SeaMonkey/2.5


Duh.

I could use case.

case "$TEST1" in
        2|3|4|5|6|7)
                echo YES
        ;;
        *)
                echo NO
        ;;
esac



Jesse Molina wrote:

Greetings all

I am trying to remember some goofy/clever way to do something. Is there
another way to do this, or any alternate methods which you prefer?

I'm working on test conditional expressions via []. I was writing a
script where I needed to evaluate and take action for certain multiple
exit codes of some program.

Let us say;

TEST1=7



The first way to do this uses single [] brackets and I'll break it up
for readability;

if [ \
"$TEST1" == 2 \
-o "$TEST1" == 3 \
-o "$TEST1" == 4 \
-o "$TEST1" == 5 \
-o "$TEST1" == 6 \
-o "$TEST1" == 7 \
] ; then
echo YES
else
echo NO
fi



But this is obviously long and annoying if I have many things to match
against. I really just want to match against a regex.

Then, I remembered that you could use =~ to do something like that, but
I had to google up to remember that you can only do it in double [[]]
brackets (new style);

if [[ "$TEST1" =~ 2|3|4|5|6|7 ]] ; then
echo YES
else
echo NO
fi



However, I seem to remember having done this previously with single
brackets, in a clever manner that was neat and didn't need as many lines
or repetitive goop as the first method mentioned above. Anyone have a
clue WTF I'm talking about, because I sure don't.

Any other clever ways to do complex conditional tests, or neat tricks on
the subject?




--
# Jesse Molina
# Mail = address@hidden
# Page = address@hidden
# Cell = 1.602.323.7608
# Web  = http://www.opendreams.net/jesse/





reply via email to

[Prev in Thread] Current Thread [Next in Thread]