Q:

nested logical expression test in shell

I am trying to write a one-liner as a replacement for the below snippet.
# Check to see if the database is online; exit if not
isbooted=`grep 'Current state: Booted' serverlog.log | wc -l`
if [ $isbooted -eq 0 ]
then
   exit 
fi 

# Check to see if the database has crashed; exit if so
iscrashed=`grep 'Status: OK' serverlog.log | wc -l`
if [ $iscrashed -eq 0 ]
then 
   exit 
fi

echo 0

This is what I have done so far (I am unsure if this is correct),
   [ $(grep 'Current state: Booted' serverlog.log | wc -l) -eq 0 ] \
&& [ $(grep 'Status: OK' serverlog.log | wc -l) -eq 0 ]            \
&& echo 0

Can you please help me here?

A:

The idea seems ok, but I'd go with a simplified version:
grep -q 'Current state: Booted' serverlog.log && grep -q 'Status: OK' serverlog.log && echo 0

or alternatively (you can replace -ge with -eq if you expect both lines to occur exactly once):
[ "$(grep -c -e 'Current state: Booted' -e 'Status: OK' serverlog.log)" -ge 2 ] && echo 0

Both versions print 0 if serverlog.log contains both 'Current state: Booted' and Status: OK.
For further reference see grep(1).

