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
    • 最新
    • 标签
主页 / unix / 问题 / 448323
Accepted
daisy
daisy
Asked: 2018-06-07 19:09:48 +0800 CST2018-06-07 19:09:48 +0800 CST 2018-06-07 19:09:48 +0800 CST

捕获并收集脚本输出,“输入文件是输出文件”错误?

  • 772

我需要上传当前脚本的输出,所以我添加了一个trapand set -ex,例如

#!/bin/bash

exec &> /tmp/error.log
trap 'cat /tmp/error.log; curl http://127.0.0.1/error.php?hostname=$(hostname) -F file=@/tmp/error.log' EXIT

set -ex
wtfwtf

当我执行它时,我总是收到这个错误,并且 PHP 脚本没有收到整个文件

%> cat /tmp/error.log
1.sh: line 6: wtfwtf: command not found
cat: /tmp/error.log: input file is output file

到目前为止,唯一的解决方案是将error.log复制到一个新文件并上传,例如

#!/bin/bash

exec &> /tmp/error.log
trap 'cp /tmp/error.log 123; curl http://127.0.0.1/error.php?hostname=$(hostname) -F file=@123' EXIT

set -ex
wtfwtf

有没有更好的方法来做到这一点?

bash file-descriptors
  • 1 1 个回答
  • 1002 Views

1 个回答

  • Voted
  1. Best Answer
    Kusalananda
    2018-06-07T22:42:08+08:002018-06-07T22:42:08+08:00

    使用exec,您将脚本的所有输出重定向到特定的日志文件。

    在您的陷阱中,您希望使用cat. 由于所有输出也被重定向到该文件,GNUcat注意到它的输入文件和标准输出流(从 shell 继承)是同一个东西,并拒绝执行它的任务。

    BSDcat不做与 GNU 相同的检查cat,如果脚本没有被中断,则会导致一个无限大的日志文件,其中同样的几行一遍又一遍地重复。

    一种解决方法是保存原始标准输出文件描述符,像以前一样进行重定向,然后在陷阱中恢复它。

    #!/bin/bash
    
    exec 3>&1                  # make fd 3 copy of original fd 1
    exec >/tmp/error.log 2>&1
    
    # in the trap, make fd 1 copy of fd 3 and close fd 3 (i.e. move fd 3 to fd 1)
    trap 'exec 1>&3-; cat /tmp/error.log; curl "http://127.0.0.1/error.php?hostname=$(hostname)" -F file=@/tmp/error.log' EXIT
    
    set -ex
    wtfwtf
    

    这会在将文件描述符 1(作为 fd 3)重定向到日志文件之前对其进行复制。在陷阱中,我们将此副本移回 fd 1 并进行输出。

    请注意,在此示例中,陷阱中的标准错误流仍连接到日志文件。因此,如果curl生成诊断消息,这将保存在日志文件中,而不是显示在终端(或原始标准错误流连接到的任何位置)上。


    考虑到 Stéphane Chazelas 的评论:

    #!/bin/sh
    
    exit_handler () {
        # 1. Make standard output be the original standard error
        #    (by using fd 3, which is a copy of original fd 2)
        # 2. Do the same with standard error
        # 3. Close fd 3.
        exec >&3 2>&3 3>&-
        cat "$logfile"
        curl "some URL" -F "file=@$logfile"
    }
    
    logfile='/var/log/myscript.log'
    
    # Truncate the logfile.
    : >"$logfile"
    
    # 1. Make fd 3 a copy of standard error (fd 2)
    # 2. Redirect original standard output to the logfile (appending)
    # 3. Redirect original standard error to the logfile (will also append)
    exec 3>&2 >>"$logfile" 2>&1
    
    # Use shell function for exit trap (for neatness)
    trap exit_handler EXIT
    
    set -ex
    wtfwtf
    

    他的观点是日志文件无论如何只用于诊断消息,因此将日志文件输出到原始标准错误流更有意义。

    他还指出,在全局可写目录中使用固定文件名是很危险的,例如/tmp. 这是因为脚本中没有进行任何检查以确保该文件不存在(例如,某人或某些恶意软件可能创建了指向您或您的/tmp/error.log符号链接)。他对此的解决方案是使用专门的持久化日志文件代替下的脚本(该文件是持久化的,但运行脚本时内容会被清除)。/etc/passwd~/.bashrc/var/log

    对此的一种变体是用于mktemp在下面创建一个唯一的文件名$TMPDIR(然后在EXIT陷阱中删除该文件,除非curl失败,在这种情况下rm不会执行,因为set -e它已经生效):

    #!/bin/sh
    
    exit_handler () {
        # 1. Make standard output be the original standard error
        #    (by using fd 3, which is a copy of original fd 2)
        # 2. Do the same with standard error
        # 3. Close fd 3.
        exec >&3 2>&3 3>&-
        cat "$logfile"
        curl "some URL" -F "file=@$logfile"
        rm -f "$logfile"
    }
    
    logfile=$( mktemp )
    
    # 1. Make fd 3 a copy of standard error (fd 2)
    # 2. Redirect original standard output to the logfile (appending)
    # 3. Redirect original standard error to the logfile (will also append)
    exec 3>&2 >>"$logfile" 2>&1
    
    # Use shell function for exit trap (for neatness)
    trap exit_handler EXIT
    
    set -ex
    wtfwtf
    

    您的第二个示例有效,但这只是因为您没有cat在日志文件上使用,而不是因为复制它。


    次要的挑剔:命令行上的 URL 可能应该总是至少用双引号引起来,因为它们往往包含 shell 可能解释为特殊的字符(例如?)。

    • 6

相关问题

  • 通过命令的标准输出以编程方式导出环境变量[重复]

  • 从文本文件传递变量的奇怪问题

  • 虽然行读取保持转义空间?

  • `tee` 和 `bash` 进程替换顺序

  • 运行一个非常慢的脚本直到它成功

Sidebar

Stats

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

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    ssh 无法协商:“找不到匹配的密码”,正在拒绝 cbc

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    如何卸载内核模块“nvidia-drm”?

    • 13 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Wong Jia Hau ssh-add 返回:“连接代理时出错:没有这样的文件或目录” 2018-08-24 23:28:13 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve