find:如何不存在错误(标准错误)

我有一个自定义脚本,当他们有一天gzip日志文件。 在我的脚本中,所有的错误(stderr)都被logging到一个文件中,并且在脚本结尾我使用这个文件来知道它的执行是否正确。

我的代码的这部分看起来像

# redirection of stderr to file exec 2> /tmp/errors.txt # gzip old log files find *.log -mtime +1|xargs gzip -f # test if error log file is empty if [ -s /tmp/errors.txt ]; then ..... fi 

我的问题是:如果至less有一个文件要gzip,这段代码工作的很好,但如果不是这样的话,那么当我不想要的时候会引发一个错误。

我试过不同的解决scheme来解决这个问题没有成功。

有谁知道如何解决这个问题?

尝试这个

 #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` 

如果您使用的是xargs的GNU版本,则可以使用-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. 

码:

 # 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