How to prevent grep command from throwing a system trap if No match is found.

Hi

How to prevent grep command from throwing a system trap(or returning error status) if No match is found in the specified file(s) ?

Consider this simple shell script:

#!/usr/bin/ksh
trap 'STATUS=$?;set +x;echo;echo error $STATUS at line nb $LINENO executing :\
   `sed -n "${LINENO}p" $0`;echo;exit $STATUS' ERR
#====== MAIN STARTS=========
cat server* | grep "ABC" > logfile.log

Now if the files server* dont have matching string "ABC", it throws the trap and gets caught, which I dont want to happen.
I want the file logfile.log to be empty if no match is found.
But if, the file server* dont even exist, then I want trap to catch it and show the error.

Please help...

My first guess is to test for existence first, then do the part that bothers you without the trap.

I agree with yegolev. Using trap in this way is unusual. ERR is only triggered if the last command in a pipeline fails. Also your script probably needs to append to the log rather than overwrite it. Here is an example:

#!/bin/ksh
trap 'echo "Trap fired";exit' ERR
ls server* 2>/dev/null 1>/dev/null # Fire trap if file is missing
#
(
ls server* | while read filename
do
grep "ABC" "${filename}"
done
) 2>&1 >> logfile.log