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


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to prevent grep command from throwing a system trap if No match is found.
# 1  
Old 01-08-2009
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...
# 2  
Old 01-08-2009
My first guess is to test for existence first, then do the part that bothers you without the trap.
# 3  
Old 01-08-2009
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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

2. Shell Programming and Scripting

Bash - trap error file not found

Hello. In bash, is there a way to trap error "file not found" when a script call another script which is not found; then abort. Example ( part of script running with -x option set) : + return 0 + RETURN_CODE=0 + ] + /root/bin/200_yast_install/00_reset_yast_install bash:... (5 Replies)
Discussion started by: jcdole
5 Replies

3. Shell Programming and Scripting

Grep everything between two pattern if match not found

I need to help to work this Print everything between 2 patterns if grep is not found the search word example Detroit orange cat bat rat apple sed -n "/Detroit,/apple/p" d |grep chicago output would be Detroit orange cat bat rat (1 Reply)
Discussion started by: jhonnyrip
1 Replies

4. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

5. Shell Programming and Scripting

kill a process if grep match is found

Hi, I need something unusual, I guess. I need to start a process, and if that process displays a specific error message, I need to kill that process and restart it. Something like: startprocess | grep -i "This is the specific error message" && kill $pidof(startprocess) Explanation, I need... (4 Replies)
Discussion started by: burek
4 Replies

6. Shell Programming and Scripting

sendmail script throwing an error "No recipient addresses found in header"

Hi, I am using following code to send an e-mail with attachment and body. echo "To: user1@mail.com,user2@mail.com" > mail.tmp echo "Cc: user3@mail.com,user4@mail.com" >> mail.tmp echo "From: group@mail.com" >> mail.tmp echo "Subject: my report" >> mail.tmp echo "please see as attached"... (6 Replies)
Discussion started by: vivek_damodaran
6 Replies

7. Shell Programming and Scripting

fetch last line no form file which is match with specific pattern by grep command

Hi i have a file which have a pattern like this Nov 10 session closed Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty . Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk . Nov 10 afrtetryytr Nov 10 session closed Nov 10 Nov 10 03:21:04 EST5EDT 2010 Dec 8 Nov 10 05:03:02 EST5EDT 2010 ... (13 Replies)
Discussion started by: Himanshu_soni
13 Replies

8. UNIX for Dummies Questions & Answers

Grep and display n lines after the match is found.

Hello, How do I use grep to find a pattern in a list of file and then display 5 lines after the pattern is matched Eg: I want to match the string GetPresentCode in all files in a folder and then see 4 lines following this match. I am not sure if grep is what should be used to achieve. Thanks!... (3 Replies)
Discussion started by: cv_pan
3 Replies

9. Shell Programming and Scripting

How do I prevent cron from returning errors on a file not found?

find: /home/Upload/*: No such file or directory I am getting a find error when I run for FILE in `find /home/Upload/* -prune -type f -newer TIMEFILE` do I need to run this job every 10 minutes to upload any new files that were added. Is there an easy way to prevent this? Thanks (6 Replies)
Discussion started by: goodmis
6 Replies
Login or Register to Ask a Question