AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 540460
Accepted
daks
daks
Asked: 2013-09-21 04:24:42 +0800 CST2013-09-21 04:24:42 +0800 CST 2013-09-21 04:24:42 +0800 CST

查找:不存在文件时如何不出现错误(stderr)

  • 772

我有一个自定义脚本,当它们一天前 gzip 日志文件。在我的脚本中,所有错误(stderr)都记录到一个文件中,在脚本的最后,我使用这个文件来了解它的执行是否良好。

我的这部分代码看起来像

# 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

我的问题是:如果至少有一个要 gzip 的文件,则此代码效果很好,但如果没有,它会在我不想这样做时引发错误。

我尝试了不同的解决方案来解决这个问题,但没有成功。

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

linux
  • 2 2 个回答
  • 1063 Views

2 个回答

  • Voted
  1. Best Answer
    Pablo
    2013-09-21T05:09:27+08:002013-09-21T05:09:27+08:00

    尝试这个

    #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`
    
    • 1
  2. Lesmana
    2013-09-24T13:40:48+08:002013-09-24T13:40:48+08:00

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

相关问题

  • Linux 主机到主机迁移

  • 如何在 Linux 机器上找到有关硬件的详细信息?

  • 如何在 Linux 下监控每个进程的网络 I/O 使用情况?

  • 在 RHEL4 上修改 CUPS 中的现有打印机设置

  • 为本地网络中的名称解析添加自定义 dns 条目

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve