你怎么能中DIFF猛砸两条管线? [英] How can you diff two pipelines in Bash?

查看:146
本文介绍了你怎么能中DIFF猛砸两条管线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么能差异两条管道而不击使用临时文件?假设你有两个命令管道:

How can you diff two pipelines without using temporary files in Bash? Say you have two command pipelines:

foo | bar
baz | quux

和你想找到差异在它们的输出。一种解决方案显然是:

And you want to find the diff in their outputs. One solution would obviously be to:

foo | bar > /tmp/a
baz | quux > /tmp/b
diff /tmp/a /tmp/b

是否有可能这样做而不猛砸使用临时文件?您可以在管线差异之一摆脱管道由单一时文件:

Is it possible to do so without the use of temporary files in Bash? You can get rid of one temporary file by piping in one of the pipelines to diff:

foo | bar > /tmp/a
baz | quux | diff /tmp/a -

但你不能既管进入管道同时差异(没有任何明显的方式,至少)。有一些聪明的把戏,涉及的/ dev / FD 来做到这一点,而不使用临时文件?

But you can't pipe both pipelines into diff simultaneously (not in any obvious manner, at least). Is there some clever trick involving /dev/fd to do this without using temporary files?

推荐答案

2个tmp文件,(不是你想要的)会是一个单行:

A one-line with 2 tmp files (not what you want) would be:

 foo | bar > file1.txt && baz | quux > file2.txt && diff file1.txt file2.txt

庆典,你可以尝试,但:

 diff <(foo | bar) <(baz | quux)

提到BenM的详细解答&LT; 创建匿名的命名管道 - 由bash的管理 - 让他们创建并自动销毁,不像临时文件结果
但是,丹尼尔·卡西迪指出了不使用临时文件的问题的一部分是不尊重:文件系统仍然是修改(与目录条目重新presenting命名管道的创建,然后取出)

As mentioned in the BenM's detailed answer, < creates anonymous named pipes -- managed by bash -- so they are created and destroyed automatically, unlike temporary files.
However, Daniel Cassidy points out the "without using temporary files" part of the question is not respected: the file system is still modified (with a directory entry representing the named pipe created and then removed)

否则,就像你在你的问题提了,你必须使用 - 作为标准输入

Otherwise, like you mention in your question, you have to use - as STDIN

 foo | bar > file1.txt && baz | quux | diff file1.txt - && rm file1.txt

,因为似乎没有简单的方法来管多输入一个命令。

, since there seems to be no easy way to pipe multiple inputs to one command.

您只能管的一个输出的到的多种输入的使用tee命令:

You can only pipe one output to multiple inputs with the tee command:

ls *.txt | tee /dev/tty txtlist.txt 

上面的命令显示LS的输出* .TXT到终端,并将其输出到文本文件txtlist.txt。

The above command displays the output of ls *.txt to the terminal and outputs it to the text file txtlist.txt.

这篇关于你怎么能中DIFF猛砸两条管线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆