【问题标题】:"command not found" message is redirected too“找不到命令”消息也被重定向
【发布时间】:2012-09-28 22:17:06
【问题描述】:
$ foobar
sh: foobar: not found
$ foobar 2>/dev/null
$ 

我正在尝试仅重定向 foobar 的 stderr(如果 foobar 存在并且可以运行),但 shell 的错误消息也会被重定向。这发生在 bash、ksh 和 sh 中,但不在 csh 中。我不熟悉 bourne shell 源代码,但我猜这是因为 shell 首先分叉,然后重定向,然后尝试 exec(),当 exec() 失败时,它会向已经重定向的 stderr 发送错误消息。

【问题讨论】:

  • 当您实际上有一个 foobar 向标准错误生成消息时, 2>/dev/null 将捕获它。使用 shell 错误代码来确定是否找到了 cmd。通常是 127 表示 cmd-not-found。关于 fork/redirect/exec 的好问题,但不知道答案。关于 S.O. 的问题来自编写自己的 shell 的学生偶尔会给出明确的命令或 fork/exec/redirect 的答案。祝你好运。

标签: bash shell


【解决方案1】:

如果 foobar 是一些可执行文件,那么试试:-

$ ./foobar
sh: foobar: not found
$ ./foobar 2>/dev/null #alternatively you can use full path.
$ 

【讨论】:

    猜你喜欢
    • 2017-08-09
    • 1970-01-01
    • 2013-02-03
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多