linux - 如何将标准压缩到文件中并将标准打印到标准输出?

标签 linux bash shell tee

我想执行一个命令,让该命令的输出得到 gzip'd 的运行,并回显/发送该命令的输出。

例如:

echo "hey hey, we're the monkees" | gzip --stdout > my_log.gz

除了行执行时,我想在标准输出上看到这个:

hey hey, we're the monkees

最佳答案

另一种方式(假设像 bashzsh 这样的 shell):

echo "hey hey, we're the monkees" | tee >(gzip --stdout > my_log.gz)

公认的奇怪的 >() 语法基本上执行以下操作:

  • 创建新的 FIFO(通常在 /tmp/ 中)
  • () 中执行命令并将 FIFO 绑定(bind)到该子命令上的标准输入
  • 将 FIFO 文件名返回到命令行。

tee 最终看到的内容类似于:

tee /tmp/arjhaiX4

gzip 看到的都是它的标准输入。

对于 Bash,请参阅 man bash详情。它在 redirection 的部分中.对于 Zsh,请参阅 man zshexpn在“进程替换”标题下。

据我所知,Korn Shell、经典 Bourne Shell 的变体(包括 ash 和 dash)以及 C Shell 不支持这种语法。

关于linux - 如何将标准压缩到文件中并将标准打印到标准输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/570984/

相关文章:

python - 我们如何使用 python 从 ssh 代理运行 tmux

php - 使用PHP通过ssh执行shell命令

linux - ack在linux中使用crontab什么都不输出

php - 返回 apache php linux 主机上区分大小写的文件

linux - awk 在 linux 中子集化失败

string - Bash 脚本 - 检查文件是否包含特定行

linux - 如何在不遗漏字符/字符串的情况下将整个代码回显到 .bashrc?

linux - ElasticBeanstalk 服务器用户/组和 `git aws.push`

java - 如何更改/分配 java .jar 的进程名称

macos - HIstory 命令只显示最后 15 个命令