如何使用 tee 和命名管道?

如何使用 tee 和命名管道?

我试图想出一个简单的命令来读取图像并显示它,同时将其写入剪贴板。这是我尝试过的方法以及失败的原因:

cat pic.png | tee >(xclip -selection clipboard -t image/png -i) | display -

这失败了,因为我无法看到显示的图像,直到xclip进程退出

maim -s | tee >(display -) | xclip -selection clipboard -t image/png

这也会失败,因为直到图片没有放入剪贴板显示过程结束

maim -s | tee >(display -) >(xclip -selection clipboard -t image/png)
maim -s | tee >(xclip -selection clipboard -t image/png) >(display -)

这两个也失败了,行为分别与情况 1 和 2 类似。

是否可以在不依赖临时文件或写入后立即读取剪贴板的情况下执行此操作?

更新:

链接tees 会产生奇怪的结果:

maim -s | tee >(display -) | tee >(xclip -selection clipboard -t image/png)

这部分有效,display显示完整图像,但剪贴板仅包含其损坏/或不完整的副本,即如果我将其粘贴到 GIMP 上,则仅粘贴 10%,其余部分只是灰色。并排示例:显示并粘贴到 GIMP 中的剪贴板内容。

在此输入图像描述

maim -s | tee >(xclip -selection clipboard -t image/png) | tee >(display -) 

这也部分有效,完整的图像被放在剪贴板上,但显示失败并出现错误:

display: no decode delegate for this image format `' @ error/constitute.c/ReadImage/566.

更新2:

好吧,以“优雅”的方式做到这一点似乎不是一个简单的方法,更不用说便携了。因此,最好的方法是分三步完成:

  1. 创建2个管道mkfifo /tmp/p1 /tmp/p2
  2. 获取图像并将其写入所述管道maim | tee /tmp/p1 /tmp/p2
  3. 同时从两个管道读取display < /tmp/p1 & xclip ... < /tmp/p2

你甚至可以用两行来做,但你明白了......

另一个解决方案是使用 xclip 的过滤器标志,来自手册页:

   -f, -filter
          when xclip is invoked in the in mode with output level set to silent (the defaults), the filter option will cause xclip to print the text piped to standard in back to standard out unmodified

所以它看起来像这样:

maim -s -f png | xclip -i -f -selection clipboard -t image/png | display

这似乎是最好的方法,也是使用 xclip 的预期方法

答案1

您需要将或多或少的整个数据缓冲在内存中。也就是说,您也可以将其写入 tmp 文件中/dev/shm

另一种选择是缓冲程序:

tee <pic.png >(buffer | xclip -selection clipboard -t image/png -i) | display -

您可能必须确保buffer使用足够的内存,以便您可以提前检查文件大小并buffer相应地设置选项。

相关内容