BASH on Redhat AND and OR with more than two conditions

lolaferrari
lolaferrari used Ask the Experts™
on
On a Redhat Linux system running a bash shell script I need some help with an if then statement that has more than 2 conditions. I basically want to check for this
A AND B or C  
A AND B or D
A AND B or E

Something along these lines but it doesn't work and wondered if I have the correct usage of brackets. It's not what's contained for evaluation that's the issue it's the syntax of the AND and OR where there's more than two conditions that I am struggling with.

if [[ $(find /opt/app -name httptd*.conf | grep -v grep | grep -c http) -eq 0 ] && [ ! -f /etc/init.d/apache ] ||  [ $(find /app -name http*.conf | grep -v grep | grep -c http) -eq 0 ]] || \
[[ $(find /opt/app -name httptd*.conf | grep -v grep | grep -c http) -eq 0 ] && [ ! -f /etc/init.d/apache ] ||  [ $(find /application -name http*.conf | grep -v grep | grep -c http) -eq 0 ]] || \
[[ $(find /opt/app -name httptd*.conf | grep -v grep | grep -c http) -eq 0 ] && [ ! -f /etc/init.d/apache ] ||  [ $(find /application -name manifest* | grep -v grep | grep -c http) -eq 0 ]] ; then
.....
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer
Distinguished Expert 2018
Commented:
you cannot mix [ [  ]  && etc.....
the [ is an alias for the program named test...

$( ) is different from ( )...

A AND B OR C ===
A && B || C   if you want to check programs... on exit status....  if you want to test variables use:

if [ "$var" == "$var2" -o "$var3" != "$var4" ] ; then....

in stead of $var you can use $( some command)   to get the OUTPUT of a command/
if  ( ( find /opt/app -name httptd*.conf | grep -v grep | grep -c http) && ( [  ! -f /etc/init.d/apache ] ) || ( find /app -name http*.conf | grep -v grep | grep -c http)  ) || \
 (  find /opt/app -name httptd*.conf | grep -v grep | grep -c http )  && ( [ ! -f /etc/init.d/apache ] ) ||  (find /application -name http*.conf | grep -v grep | grep -c http) - || \
(find /opt/app -name httptd*.conf | grep -v grep | grep -c http)  &&  ( [ ! -f /etc/init.d/apache ] ) ||  (find /application -name manifest* | grep -v grep | grep -c http) ) ; then ....

Open in new window


Might work better.
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Also, you'll be able to write better self documenting code using PERL for this.

Keep in mind... a month or year from now when you come back to your code, be sure to write it so a simple glance will tell your or someone completely new to your code, what your code does... instantly...

Author

Commented:
Delighted with the response!
I would think that the initial problem came from a slightly confusing wording.
A AND B OR C
can be a source of confusion misunderstanding between persons (and also sytems).
as would be
15 - 5  x 2
(or to be more inline with the problem 15 x 2 - 1 )

In such situations, it is usually wiser to put additional parentheses just to clarify the meaning between everybody.

In the case described here, and looking at the accepted solutions , it seems that the meaning was clearly "left to right", ie
(A AND B) OR C

In such a case, I would personnally first check (A AND B) before testing the additional "ORs"
- if (A AND B) is true, THEN NONE OF THE ORs will be checked by (A AND B) OR C!!!!!
- only if A or B is false will the ORs be tested
nociSoftware Engineer
Distinguished Expert 2018

Commented:
@bernard,
there also was another thing implied... like [[ would be equivalent to [ [ ...
problem is [ is equivalent to test (symlink on some systems) , it is not bracket... and there is no equivalent for [[
$ ls -li '/usr/bin/['
2228864 -rwxr-xr-x 1 root root  51720 10 jul 01:06 '/usr/bin/['

man test will show the arguments... To rephrase the if [[... is impossible if [ expr    ] ; then ...
if equivalent to if test expr ; then ...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial