【问题标题】:why this command not working?(pipe, redirection)为什么这个命令不起作用?(管道,重定向)
【发布时间】:2021-09-04 11:17:42
【问题描述】:
bash-3.2$ ls | grep Makefile > a.txt | cat a.txt

为什么这不起作用? “Makefile”是存在的。

【问题讨论】:

  • ls | grep Makefile > a.txt 正在工作。除了 cat a.txt
  • 我在 C 中实现管道、重定向
  • 看不到 cat a.txt 结果
  • 不知道为什么,但更好的命令是ls | grep Makefile > a.txt && cat a.txt。您没有从grepcat 命令传递任何内容,它是空的且毫无意义的。
  • 或使用 ls | grep 生成文件 | tee a.txt tee 命令将写入文件但也会打印到标准输出

标签: linux redirect pipe


【解决方案1】:

grep 命令没有输出,因为您将其重定向到文件。因此管道在cat a.txt 实际被调用之前关闭。根据我的评论,使用&& 而不是最后的|

【讨论】:

  • 更好的命令是ls Makefile | tee a.txt。为什么grep
【解决方案2】:

做你想做的事,将命令的输出保存到文件中,但仍然使用 tee 打印到标准输出。 (我也喜欢用 ls -1 来确保每行只打印一个项目,这不是必需的,而是习惯的力量)

 ls -1 | grep Makefile | tee a.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2016-10-09
    相关资源
    最近更新 更多