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 / 问题 / 488731
Accepted
tudor -Reinstate Monica-
tudor -Reinstate Monica-
Asked: 2013-03-18 20:13:08 +0800 CST2013-03-18 20:13:08 +0800 CST 2013-03-18 20:13:08 +0800 CST

什么可以阻止命令的输出?

  • 772

有时我会尝试 (2>&1) 重定向,并且部分/所有结果输出似乎被静音。

例如

wget -O- http://localhost/test.txt 2>&1

我希望看到 test.txt 的内容与传输输出的合并,但结果只是输出到 stderr 而不是输出到 stdout:

--2013-03-18 14:53:41--  http://localhost/test.txt
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9 [text/plain]
Saving to: `STDOUT'

 0% [                                       ] 0           --.-K/s              1100%     
    [======================================>] 9           --.-K/s   in 0s      

2013-03-18 14:53:41 (2.09 MB/s) - written to stdout [9/9]

stdout 不应该把它写到屏幕上吗?

但是:

wget -O- http://localhost/test.txt 2>&1 > test.stdout

导致文件按预期写入 test.stdout。

同样,我在期望脚本 (send_user) 和多个 grep 管道中看到了这种行为。例如

/myexpectscript | grep 'blah'

工作并过滤除包含“blah”的行之外的所有行,但是

/myexpectscript | grep 'foo' | grep 'bar'

导致空白输出。

当我想使用 tee 复制输出时,我是如何发现这一点的。例如

wget -O- http://localhost/test.txt 2>&1 | tee

结果根本没有输出,而:

wget -O- http://localhost/test.txt | tee

结果是:

--2013-03-18 15:16:42--  http://localhost/ddns/checkip.php
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9 [text/plain]
Saving to: `STDOUT'

127.0.0.1100%[======================================>] 9           --.-K/s   in 0s      

2013-03-18 15:16:42 (2.30 MB/s) - written to stdout [9/9]

(注意第8行列出了test.txt“127.0.0.1”的内容)

什么情况下重定向输出会被阻塞?为什么 wget 到 stdout 的假定输出仅在重定向到文件或命令时有效?

bash
  • 2 2 个回答
  • 236 Views

2 个回答

  • Voted
  1. Hauke Laging
    2013-03-18T22:11:02+08:002013-03-18T22:11:02+08:00

    这里wget -O- http://localhost/test.txt 2>&1确实将下载的内容写入标准输出。

    grep 'foo' | grep 'bar'如果没有同时包含“foo”和“bar”的行,显然不会产生任何输出。

    • 0
  2. Best Answer
    tudor -Reinstate Monica-
    2015-10-06T19:46:13+08:002015-10-06T19:46:13+08:00

    由于升级了几次,我发现现在的行为符合预期。

    $ wget -O- http://localhost/test.txt 2>&1
    --2015-10-06 14:39:58--  http://localhost/test.txt
    Resolving localhost (localhost)... 127.0.0.1
    Connecting to localhost (localhost)|127.0.0.1|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 10 [text/plain]
    Saving to: 'STDOUT’
    
    -                             0%[                                             ]       0  --.-KB/s             It works!
    -                           100%[============================================>]      10  --.-KB/s   in 0s     
    
    2015-10-06 14:39:58 (918 KB/s) - written to stdout [10/10]
    

    如您所见,在第一行进度条的末尾“It works!” (文本文件的内容)与输出正确合并。

    面对任何其他合理的解释,我只能得出结论,这一定是 bash 或装饰性的终端错误。

    • 0

相关问题

  • Mac OS X:从 python 脚本中更改 $PATH

  • Bash 脚本:要求脚本以 root 身份运行(或使用 sudo)

  • crontab ifconfig 什么都不输出

  • 使用命令行工具按排序顺序计算重复项

  • 是否有 bash 等效于 ruby​​ 的“一些内容#{foo}”?

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