How to prevent grep command from throwing a system trap if No match is found. | Unix Linux Forums | UNIX for Advanced & Expert Users

  Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

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

UNIX for Advanced & Expert Users


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-08-2009
cool.aquarian cool.aquarian is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 1 February 2010, 7:49 AM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Question 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:


Code:
#!/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...
Sponsored Links
    #2  
Old 01-08-2009
Yegolev Yegolev is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 8 January 2009, 4:03 PM EST
Location: The ATL
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
My first guess is to test for existence first, then do the part that bothers you without the trap.
Sponsored Links
    #3  
Old 01-08-2009
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 18 April 2014, 5:13 AM EDT
Posts: 6,396
Thanks: 287
Thanked 673 Times in 642 Posts
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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep and display n lines after the match is found. cv_pan UNIX for Dummies Questions & Answers 3 09-25-2008 11:15 PM
if match found go to a particular line in perl user_prady Shell Programming and Scripting 17 03-31-2008 03:00 PM
Remote login/copy command throwing an error ashish_panpalia UNIX for Dummies Questions & Answers 1 02-12-2008 04:04 AM
sccs get -ek command is throwing err Shivaprasad BS Shell Programming and Scripting 0 01-31-2008 04:49 AM
How do I prevent cron from returning errors on a file not found? goodmis Shell Programming and Scripting 6 02-06-2007 09:14 AM



All times are GMT -4. The time now is 04:40 AM.