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
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1081337
Accepted
Bradley Odell
Bradley Odell
Asked: 2018-10-06 18:33:54 +0800 CST2018-10-06 18:33:54 +0800 CST 2018-10-06 18:33:54 +0800 CST

Bash 文件重定向错误?

  • 772

根据我对程序 IO 的了解,有stdin、stdout和stderr数据流(以及返回码)。是两个数据输出流stdout。stderr因此,如果我使用 bash 重定向来关闭其中一个输出流,我可以缩小将文本发送到哪个流的范围。正确的?

我正在运行 Ubuntu 18.04.1 LTS,并且遇到了 bash 重定向这个奇怪的问题。

让我解释一下这个例子。这是我的命令:

# apt-cache show php5
N: Can't select versions from package 'php5' as it is purely virtual
N: No packages found
# |

该php5软件包在 Ubuntu 18.04 上不存在,因此apt-cache显示错误。我会假设这个文本被发送到stderr流,所以我试图关闭那个流:

# apt-cache show php5 2>&-
# |

这似乎验证了文本是通过 发送的stderr。到目前为止一切都很好!但现在作为健全性检查,我尝试关闭stdout(我现在应该看到错误文本):

# apt-cache show php5 1>&-
# |

什么!?stdout这次我重定向了,但stderr也没有出现?

根据互联网,我的文件描述符正确:https ://www.gnu.org/software/bash/manual/html_node/Redirections.html

我不知道这里会发生什么。

截图证明:

Bash 重定向错误

bash redirect stdout
  • 1 1 个回答
  • 356 Views

1 个回答

  • Voted
  1. Best Answer
    Sergiy Kolodyazhnyy
    2018-10-06T19:31:13+08:002018-10-06T19:31:13+08:00

    TL;DR:不是bash,而是apt-cache文件描述符乱七八糟。

    apt-cache正在做一些非常有趣的事情 - 它往往不会写出以N:用于标准输出的字符开头的行。

    考虑一下:

    $ apt-cache show nonexistent
    N: Unable to locate package nonexistent
    E: No packages found
    

    我们看到两条线,一条以 .N:开头E:。N:行转到标准输出。在您的示例中,您有两N:行。

    # apt-cache show php5
    N: Can't select versions from package 'php5' as it is purely virtual
    N: No packages found
    

    如果您通过跟踪系统调用,strace -e write -f bash -c 'apt-cache show randomtext >&-'您会看到写入E:行发生,但N行不存在:

    [pid 12450] write(2, "E", 1E)            = 1
    [pid 12450] write(2, ": ", 2: )           = 2
    [pid 12450] write(2, "No packages found", 17No packages found) = 17
    [pid 12450] write(2, "\n", 1
    )           = 1
    [pid 12450] +++ exited with 100 +++
    --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=12450, si_uid=1000, si_status=100, si_utime=5, si_stime=2} ---
    +++ exited with 100 +++
    

    所以apt-cache很聪明,可以检查重定向的标准输出。但是呢stderr?显然写仍然存在:如果你这样做 strace -e write,openat,dup2 -f bash -c 'apt-cache show randomtext 2>&-,你会看到apt-cache打开/dev/null仍然有一些东西存在stderr:

    [pid 12543] openat(AT_FDCWD, "/dev/null", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 2
    ....
    [pid 12543] write(2, "N", 1)            = 1
    [pid 12543] write(2, ": ", 2)           = 2
    [pid 12543] write(2, "Unable to locate package randomt"..., 35) = 35
    [pid 12543] write(2, "\n", 1)           = 1
    [pid 12543] write(2, "E", 1)            = 1
    [pid 12543] write(2, ": ", 2)           = 2
    [pid 12543] write(2, "No packages found", 17) = 17
    [pid 12543] write(2, "\n", 1)           = 1
    [pid 12543] +++ exited with 100 +++
    --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=12543, si_uid=1000, si_status=100, si_utime=5, si_stime=3} ---
    +++ exited with 100 +++
    

    如果您在 bash 中对其他程序执行相同的操作,它会按预期工作:

    # stdout closed, stderr not
    $ ls -l /proc/self/fd >&-
    ls: write error: Bad file descriptor
    # stdout open , stderr closed, and it's number is assigned to whatever command is trying to open - in this case /proc/self/fd directory
    $ ls -l /proc/self/fd 2>&-
    total 0
    lrwx------ 1 xie xie 64 Oct  6 11:32 0 -> /dev/pts/1
    lrwx------ 1 xie xie 64 Oct  6 11:32 1 -> /dev/pts/1
    lr-x------ 1 xie xie 64 Oct  6 11:32 2 -> /proc/12723/fd
    
    • 3

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

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

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve