【问题标题】:Why can't I execute some command in Linux without any output in the terminal?为什么我不能在 Linux 中执行某些命令而终端没有任何输出?
【发布时间】:2013-08-14 09:28:35
【问题描述】:

我尝试在终端中执行“rmdir *”。因为有些不是当前文件夹下的目录,所以会显示一些文件不是目录的错误消息。我的目标是静默执行命令而没有任何输出。

我尝试了以下方法:

rmdir * > /dev/null
rmdir * > file.txt
rmdir * |grep "noexist"  (some non-existing characters)
rmdir * 2>&1 > /dev/null
rmdir * 2>&1 > file.txt
rmdir * 2>&1 |grep "noexist"

只有最后一个命令有效!我觉得有点困惑。由于最后一个命令有效,为什么第 4 和第 5 个命令不起作用?

【问题讨论】:

    标签: linux command


    【解决方案1】:

    > 重定向 STDOUT。 2> 重定向 STDERR。 &1 重定向到 STDOUT。 | 将 STDOUT 传输到新进程的 STDIN。

    你试过了吗:

    rmdir * > /dev/null 2> /dev/null
    

    这会将 STDOUT 和 STDERR 都重定向到 null 设备。

    【讨论】:

    • 这太啰嗦了。 rmdir * >/dev/null 2>&1 就可以了。
    【解决方案2】:
    1) Doesn't redirect stderr using 2>...
    2) See 1.
    3) See 2.
    4) Redirects stderr to stdout, and stdout is redirected to /dev/null, so you still see the redirected stderr on stdout.
    5) See 4. The file to which stdout is redirected is just different.
    6) This works because you redirected stderr to stdout. stdout is piped in to grep as grep's stdin.
    

    真正有效的方法:

    rmdir * >/dev/null 2>&1
    

    为什么会这样?因为第一个标准输出被重定向到 /dev/null。然后stderr被重定向到stdout,而stdout已经被重定向到了/dev/null。

    【讨论】:

    • 感谢您的回复。非常清楚。但是另一个人首先回答:>顺便说一句,我发现我可以使用 rmdir * 2 > /dev/null。所以我认为在我的情况下,没有标准输出:>
    【解决方案3】:

    在目录为空之前,您不能使用 rmdir.. 确保.. 在执行您的操作之前..

    【讨论】:

      猜你喜欢
      • 2019-10-31
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 2014-05-15
      • 2019-06-09
      • 1970-01-01
      相关资源
      最近更新 更多