?
Solved

grep if statement

Posted on 2011-10-26
7
Medium Priority
?
562 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:telliot79
7 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 1200 total points
ID: 37031658
#!/bin/sh
if grep "return" CNB_GAT.out
then
    echo "Found"
else
        echo " Not Found"
fi

0
 
LVL 31

Expert Comment

by:farzanj
ID: 37031663
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
 
LVL 31

Expert Comment

by:farzanj
ID: 37031709
Sorry change == to >
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37031711
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
 
LVL 19

Expert Comment

by:xterm
ID: 37031726
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
 
LVL 31

Expert Comment

by:farzanj
ID: 37031729
Already did.  I am simply counting.
0
 

Author Closing Comment

by:telliot79
ID: 37031740
chair and user almost went out 3rd floor window.

thanks for the help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question