LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-21-2010, 06:19 AM   #1
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Rep: Reputation: Disabled
Bash script: catch file not found error and send to /dev/null


I have the following working script. It checks the directory for txt files, if files are there, it copies to another directory or gives error. I would like to exclude "file not found" errors and send them to /dev/null. All other errors should go to the email address as usual.

Thanks.

Code:
#!/bin/bash

function err
{
 if [[ $? -ne 0 ]]
    then
       cat /var/log/error.log | mail -s "script on `hostname` failed"
a@b.com
    exit 1
 fi
}
cp /var/mkt/*.txt /media/nfs
err
cat /var/log/stdout.log | mail -s "script ran successfully on `hostname`"
a@b.com
exit 0
 
Old 04-21-2010, 06:24 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
A quick thought, you can test wether the *.txt file exists in the directory if yes the process else stop the script there only.
 
Old 04-21-2010, 06:48 AM   #3
GazL
Senior Member
 
Registered: May 2008
Posts: 3,392

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Instead of trying to trap a file not found, you're much better off avoiding trying to copy things that
aren't there in the first place. Replacing you cp command with the following should do it:
Code:
find /var/mkt -maxdepth 1 -name "*.txt" -print0 | xargs -0r cp -t /media/nfs
find -print0 and xarg -0 options so that it correctly handles filenames containing spaces.
xargs -r option so that it doesn't try and run the cp command if there is no input.

Last edited by GazL; 04-21-2010 at 06:59 AM.
 
Old 04-21-2010, 10:50 AM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
POSIX find can do the same sort of things with -exec +
Code:
find /var/mkt -maxdepth 1 -name "*.txt" -exec cp "{}" /media/nfs +
In my experience xargs is almost always pointless because it's only really ever used with find, and -exec + gets the exact same effect as using xargs.
 
Old 04-21-2010, 11:32 AM   #5
GazL
Senior Member
 
Registered: May 2008
Posts: 3,392

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by tuxdev View Post
POSIX find can do the same sort of things with -exec +
Code:
find /var/mkt -maxdepth 1 -name "*.txt" -exec cp "{}" /media/nfs +
In my experience xargs is almost always pointless because it's only really ever used with find, and -exec + gets the exact same effect as using xargs.
I wasn't aware of the "-exec +" but the above doesn't work for me.

From a quick test, it would seem that the command fails if '{}' isn't the last parameter on the command line passed to -exec (which makes sense if you read what the man-page says it does), so you'd still need to use the "-t" flag on the cp as I did with the xargs based solution I posted.

So, it would be.
Code:
find /var/mkt -maxdepth 1 -name "*.txt" -exec cp -t /media/nfs '{}' +
Anyway, I've learnt something new here so thanks for posting tuxdev. As ever there's always several ways of doing something on the shell command line. My gut feeling is that I still prefer the use of xargs, but that may just be a familiarity issue.
 
Old 04-24-2010, 08:52 AM   #6
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,019

Rep: Reputation: 63
Quote:
Originally Posted by noir911 View Post
I have the following working script. It checks the directory for txt files, if files are there, it copies to another directory or gives error. I would like to exclude "file not found" errors and send them to /dev/null. All other errors should go to the email address as usual.

Thanks.

Code:
#!/bin/bash

function err
{
 if [[ $? -ne 0 ]]
    then
       cat /var/log/error.log | mail -s "script on `hostname` failed"
a@b.com
    exit 1
 fi
}
cp /var/mkt/*.txt /media/nfs
err
cat /var/log/stdout.log | mail -s "script ran successfully on `hostname`"
a@b.com
exit 0
You can check if there is any text files or not or check the post/GazL's post above this post.
 
Old 04-24-2010, 12:24 PM   #7
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,758

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
Surprisingly, using xargs is most likely faster. I had a loop which was running lots of find -exec stuff which was really slow. Changing it to use xargs speeded it up by several times.
 
Old 04-24-2010, 08:37 PM   #8
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,019

Rep: Reputation: 63
Quote:
Originally Posted by gnashley View Post
Surprisingly, using xargs is most likely faster. I had a loop which was running lots of find -exec stuff which was really slow. Changing it to use xargs speeded it up by several times.
May be,-exec is used more than xargs with find command.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
output to /dev/null in shell script for multiple processes Ichabod7 Linux - Newbie 2 12-22-2009 11:38 AM
how to execute a script file? Have file/directory not found error sirius57 Linux - Software 2 11-21-2007 11:43 PM
What is meant by " file > /dev/null 2>&1 </dev/null " attockonian Linux - Newbie 5 06-30-2006 10:51 PM
Send X Windows / GUI to /dev/null? mattfr Linux - General 3 11-17-2004 07:40 AM
Creating a printer, but sending to a script & /dev/null, how to? cougar694u Linux - Software 1 09-24-2004 01:38 PM


All times are GMT -5. The time now is 01:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration