Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using fgrep with 'if' in shell scripts

Status
Not open for further replies.

Fozzeebear

Technical User
Oct 20, 2003
6
0
0
GB
I am trying (and unfortunately failing!) to write a short Unix script to do the following:

1) do a fgrep search for required strings within a file
2) if there are matches, do nothing
3) if there are no matches, print an error message

The sort of thing I have been trying is:

if test 'fgrep -f list filename' = 0 ;
then echo "error";
fi

But, this does not work. Any help (or suggestions for a simpler approach) will be much appreciated...
 
It is suggested to use grep -F instead of fgrep

try
if [ `grep -F -f list filename > /dev/null` = 0 ] ; then
echo "error"
fi

or
grep -F -f list filename > /dev/null
if [ $? = 0 ] ; then
echo "error"
fi
 
Try something like this:
Code:
fgrep -f list filename >/dev/null || echo "error"
The man page of fgrep should give you the exit codes.

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top