【问题标题】:How do I write to a file and print to a terminal concurrently in Unix?如何在 Unix 中同时写入文件并打印到终端?
【发布时间】:2011-02-24 10:25:53
【问题描述】:

我有一个小 bash 函数可以将我的 Macports 输出记录到文件中(因为安装经常会喷出一些很容易在终端噪音中丢失的小花絮),然后我只是将文件放到终端:

function porti {
    command sudo port install $@ >> $1.log 2>&1; cat $1.log
}

有没有办法同时做到这一点?

顺便说一句我通过 $@ 进行安装,但文件名只传递了 $1,以便我可以执行以下操作:

porti git-gore +bash_completion

并且只获取文件 git-core.log 但是其他人可能更喜欢在文件名中包含变体...

【问题讨论】:

    标签: bash file-io terminal unix macports


    【解决方案1】:

    通常的解决方案是使用 tee(1)

    sudo port install $@ 2>&1 | tee -a $1.log
    

    应该做你想做的事

    【讨论】:

    • 你们这些疯狂的 Unix 人让我大吃一惊!你是怎么知道 tee(1) 命令的?
    • tee 是标准 shellutils GNU 软件包的一部分,其中包含大约 2 打在 shell 脚本中有用的随机命令,并且已经存在了几十年
    猜你喜欢
    • 2016-07-26
    • 1970-01-01
    • 2018-07-24
    • 2012-03-08
    • 2022-11-02
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    相关资源
    最近更新 更多