【问题标题】:Redirecting piped command into a file in bash将管道命令重定向到bash中的文件
【发布时间】:2015-11-10 17:53:43
【问题描述】:

我正在尝试执行以下操作:

ping some.server.com | grep -Po '(?<=\=)[0-9].\.[0-9]' >> file.dat

即我运行一个命令(ping),grep 部分输出并将 grep 的结果重定向到一个文件中以供稍后检查。虽然命令本身有效(即“>>”之前的部分),但没有任何内容写入文件。

我该如何正确地做到这一点?

【问题讨论】:

  • 你等得够久了吗?在我看来像一个缓冲问题(即,文件不会被写入,例如,在标准输出中有 4KB;过早中断^C 也不会写入结果)。

标签: bash pipe command-line-interface io-redirection


【解决方案1】:

使用--line-buffered 参数。

ping some.server.com | grep --line-buffered -Po '(?<=\=)[0-9].\.[0-9]' >> file.dat

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-21
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2021-02-28
    相关资源
    最近更新 更多