LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script: catch file not found error and send to /dev/null (https://www.linuxquestions.org/questions/programming-9/bash-script-catch-file-not-found-error-and-send-to-dev-null-803259/)

noir911 04-21-2010 06:19 AM

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


PMP 04-21-2010 06:24 AM

A quick thought, you can test wether the *.txt file exists in the directory if yes the process else stop the script there only.

GazL 04-21-2010 06:48 AM

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.

tuxdev 04-21-2010 10:50 AM

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.

GazL 04-21-2010 11:32 AM

Quote:

Originally Posted by tuxdev (Post 3942828)
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.

cola 04-24-2010 08:52 AM

Quote:

Originally Posted by noir911 (Post 3942499)
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.

gnashley 04-24-2010 12:24 PM

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.

cola 04-24-2010 08:37 PM

Quote:

Originally Posted by gnashley (Post 3946451)
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.


All times are GMT -5. The time now is 07:55 AM.