find: how to not have errors (stderr) when no files exists
Try this
#redirect stderr to null
exec 2> /dev/null
FILECOUNT = `find *.log -mtime +1|wc -l`
#redirection of stderr to file
exec 2> /tmp/errors.txt
# gzip old log files
if [ $FILECOUNT != '0' ]; then
find *.log -mtime +1|xargs gzip -f
fi
# test if error log file is empty
if [ -s /tmp/errors.txt ]; then
.....
fi`
If you are using a GNU version of xargs you can use -r
:
--no-run-if-empty
-r If the standard input does not contain any nonblanks, do not run
the command. Normally, the command is run once even if there is
no input. This option is a GNU extension.
Code:
# redirection of stderr to file
exec 2> /tmp/errors.txt
# gzip old log files
find *.log -mtime +1|xargs -r gzip -f
# test if error log file is empty
if [ -s /tmp/errors.txt ]; then
.....
fi