grep if statement

experts,

can someone tell me what's wrong with this if statement:

#!/bin/sh
if grep "return" CNB_GAT.out
    echo "Found"
else
        echo " Found"
fi

I keep recieving the following error:

"Syntax error at line 5 : `else' is not expected."

i've literally copy and pasted this from a website and can't get it working. i'm using hpux 11.11.
telliot79Asked:
Who is Participating?
 
woolmilkporcConnect With a Mentor Commented:
#!/bin/sh
if grep "return" CNB_GAT.out
then
    echo "Found"
else
        echo " Not Found"
fi

0
 
farzanjCommented:
Try this one
#!/bin/sh
if (( $(grep -c  "return" CNB_GAT.out) == 0 ))
then
    echo "Found"
else
        echo "Not Found"
fi

Open in new window

0
 
farzanjCommented:
Sorry change == to >
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
woolmilkporcCommented:
Not quite, farzanj

Exchange "Found" and "Not Found" and you're OK.

To avoid grep's output my version should read:

#!/bin/sh
if grep -q "return" CNB_GAT.out
then
    echo "Found"
 else
        echo " Not Found"
fi


wmp
0
 
xtermCommented:
This DOES work
#!/bin/sh
if [ `grep -c return CNB_GAT.out` -gt 0 ]; then
    echo "Found"
else
    echo "Not Found"
fi

Open in new window

0
 
farzanjCommented:
Already did.  I am simply counting.
0
 
telliot79Author Commented:
chair and user almost went out 3rd floor window.

thanks for the help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.