本文介绍了为什么不将文件名列表通过管道传输到 cat 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cat 不从管道输入中获取文件名列表的设计原理是什么?为什么设计师选择以下方法不起作用?

What is the design rationale that cat doesn't take list of file names from pipe input? Why did the designers choose that the following does not work?

ls *.txt | cat

相反,他们选择我们需要将文件名作为参数传递给 cat 为:

Instead of this, they chose that we need to pass the file names as argument to cat as:

ls *.txt | xargs cat

推荐答案

当你说 ls *.txt |cat 不起作用,你应该说它不像你期望的那样工作.事实上,它的工作方式被认为是有效的.

When you say ls *.txt | cat doesn't work, you should say that doesn't work as you expect. In fact, that works in the way it was thought to work.

来自man:

cat - Concatenate FILE(s), or standard input, to standard output

假设下一个输出:

$ ls *.txt
file1.txt
file2.txt

... cat 的输入将是:

... the input to cat will be:

file1.txt
file2.txt

...这正是cat标准输出

在某些 shell 中,它相当于:

In some shells, it's equivalent to:

cat <(ls *.txt)

ls *.txt > tmpfile; cat tmpfile

所以,cat 真的像他们的设计师所期望的那样工作.

So, cat is really working as their designers expected to do so.

另一方面,您所期望的是 cat 将其输入解释为一组文件名以读取和连接它们的内容,但是当您 pipecat,该输入作为一个单独的文件.

On the other hand, what you are expecting is that cat interprets its input as a set of filenames to read and concatenate their content, but when you pipe to cat, that input works as a lonely file.

这篇关于为什么不将文件名列表通过管道传输到 cat 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:28