Nested if else

Hi,

i m trying to create script which logic is like below.

if [ -s ${LOGFILE} ]; then
x=`cat /tmp/testoutput.log | grep STOP | wc -l`
y=`cat /tmp/testoutput.log | grep RUN | wc -l`
if [ "$x" -gt 0 ]; then
echo "process stop"
if [ "$y" -gt 0 ]; then
echo "process running "
else
echo "file not found"
fi

----------------
This syntax does not work , could somebody suggest anyway to do it.

Thanks.

That is Useless Use of Cat and Useless Use of wc -l

Try this code:

if [ -f ${LOGFILE} ]; then
        x=$( grep -c STOP /tmp/testoutput.log )
        y=$( grep -c RUN  /tmp/testoutput.log )
        if [ $x -gt 0 ]; then
                echo "process stop"
        fi
        if [ $y -gt 0 ]; then
                echo "process running "
        fi
else
        echo "file not found"
fi
1 Like

Further optimization:

if grep -q STOP /tmp/testoutput.log; then
  echo "process stop"
fi
if grep -q START /tmp/testoutput.log; then
  echo "process start"
fi
1 Like