为了更好地理解 xargs
我想了解 Rampion 的代码:
screen -t man /bin/sh -c 'xargs man || read'
感谢 Rampion:我们不需要 cat!
为什么我们在命令中需要 xargs
?
我对 xargs 的理解如下:
- cat 对 xargs 没有任何内容
- xargs 生成了一个 man 命令列表
我有一个想法,xargs 生成了一个命令列表。 例如,
find . -type f -print0 | xargs -0 grep masi
与命令列表相同:
find fileA AND grep masi in it
find fileB AND grep masi in it
and so on for fileC, fileD, ...
I want to understand the use of xargs man
in Rampion's code:
screen -t man /bin/sh -c 'xargs man || read'
Thanks to Rampion: we do not need cat!
Why do we need xargs
in the command?
I understand the xargs
-part as follows
- cat nothing to xargs
- xargs makes a list of man -commands
I have had an idea that xargs makes a list of commands. For instance,
find . -type f -print0 | xargs -0 grep masi
is the same as a list of commands:
find fileA AND grep masi in it
find fileB AND grep masi in it
and so on for fileC, fileD, ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,我不会
cat
任何东西。 我会捕捉运行命令后收到的任何输入。cat
在这里实际上是无关的,所以让我们忽略它。xargs man
等待用户输入。 这是必要的。 由于在脚本中您从中获取了该内容,因此在创建窗口之前我无法粘贴 man 的参数。 因此,在窗口中运行的命令需要等待我给它一些东西,然后才能尝试运行 man。如果我们只是运行 screen /bin/sh -d 'man || read',它总是会抱怨“你想要什么手册页?” 因为我们从未告诉过它。
No, I don't
cat
nothing. I cat whatever input I get after I run the command.cat
is actually extraneous here, so let's ignore it.xargs man
waits on user input. Which is necessary. Since in the script you grabbed that from, I can't paste in the argument for man until after I create the window. So the command that runs in the window needs to wait for me to give it something, before it tries to run man.If we just ran
screen /bin/sh -d 'man || read'
, it would always complain "What manual page do you want?" since we never told it.xargs 从 stdin 收集参数并执行使用这些参数给出的命令。
所以 cat 正在等待输入某些内容,然后 xargs 正在使用该输入运行 man 。
如果您有很多文件需要处理,xargs 很有用,我经常将它与 find 的输出一起使用。
xargs 会将尽可能多的参数填充到命令行中。
非常适合做类似的事情
xargs gathers arguments from stdin and executes the command given with those arguments.
so cat is waiting for something to be typed, and then xargs is running man with that input.
xargs is useful if you have a lot of files to process, I often use it with output from find.
xargs will stuff as many arguments as it can onto the command line.
It's great for doing something like
cat
命令不会对任何东西进行操作; 它对标准输入进行操作,直到被告知输入结束。 正如 Rampion 所指出的,cat 命令在这里不是必需的,但它是对其隐式输入(标准输入)进行操作,而不是对任何内容进行操作。xargs 命令读取 cat 的输出,并将信息分组到指定为其(唯一)参数的
man
命令的参数中。 当它达到限制(可在命令行上配置)时,它将执行man
命令。查找... -print0 | xargs -0 ...
习语处理包含空格、制表符和换行符等尴尬字符的文件名。find
命令打印每个文件名,后跟一个 ASCII NUL ('\0'
); 这是不能出现在简单文件名中的两个字符之一 - 另一个是“/”(当然,它出现在路径名中,但不会出现在简单文件名中)。 它并不直接等同于您提供的序列;xargs
将文件名集合分组到单个参数列表中,最大大小限制。 如果名称足够短(通常如此),则 grep 的执行次数将少于文件名的数量。另请注意,如果有多个要搜索的文件,或者如果它支持一个选项,那么它总是打印文件名和内容,则 grep 仅打印找到材料的文件名。使用选项:“
-H
”是执行此操作的grep
的 GNU 扩展。 确保文件名始终出现的可移植方法是将/dev/null
列为第一个文件(因此“xargs grep some /dev/null
”); 搜索/dev/null
并不需要很长时间。The
cat
command does not operate on nothing; it operates on standard input, up until it is told that the input is ended. As Rampion notes, thecat
command is not necessary here, but it is operating on its implicit input (standard input), not on nothing.The
xargs
command reads the output fromcat
, and groups the information into arguments to theman
command specified as its (only) argument. When it reaches a limit (configurable on the command line), it will execute theman
command.The
find ... -print0 | xargs -0 ...
idiom deals with file names that contain awkward characters such as blanks, tabs and newlines. Thefind
command prints each filename followed by an ASCII NUL ('\0'
); this is one of two characters that cannot appear in a simple file name - the other being '/' (which appears in path names, of course, but not in simple file names). It is not directly equivalent to the sequence you provide;xargs
groups collections of file names into a single argument list, up to a size limit. If the names are short enough (they usually are), then there will be fewer executions ofgrep
than there are file names.Note, too, the
grep
only prints the file name where the material is found if it has more than one file to search -- or if it supports an option so that it always prints the file names and the option is used: '-H
' is a GNU extension togrep
that does this. The portable way to ensure that the file names always appear is to list/dev/null
as the first file (so 'xargs grep something /dev/null
'); it doesn't take long to search/dev/null
.