How can I compare the output of two unix commands

2020-06-01 07:56发布

问题:

I prefer not to create new files. I want to accomplish something similar to:

cmd1 > a
cmd2 > b
cat a b b | sort | uniq -u

but without using files a and b.

回答1:

Unix utilities are generally file oriented, so nothing quite does what you want.

However, zsh can autocreate temporary files with the following syntax:

diff =(cmd1) =(cmd2)

It can also create temporary named pipes (or use the special files /dev/fdn to reference anonymous pipes) with

diff <(cmd1) <(cmd2)

However, many diffs call lseek() on their input, so won't work with named pipes.

(diff is in general a more useful command for comparing very similar output than your pipeline above.)

See the "process substitution" section of the "zshexpn" man page for more details.



标签: unix