Unix - “xargs” - 输出“在中间” (不是最后!)
在 Unix 中使用 xargs
应用程序的示例可以是这样的:
ls | xargs echo
这与(假设我有 someFile
和 someDir/
在工作目录):
echo someFile someDir
因此 xargs 获取其输入并将其放置在下一个命令的末尾(此处位于 echo 的末尾)。
但有时我希望 xargs 将其输入放置在下一个命令中间的某个位置。
例如:
find . -type f -name "*.cpp" -print | xargs g++ -o outputFile
所以如果我在当前目录中有文件 a.cpp
、b.cpp
、c.cpp
输出将是相同的与命令一样:
g++ -o outputFile a.cpp b.cpp c.cpp
但我想要这样的东西:
g++ a.cpp b.cpp c.cpp -o outputFile
有办法做到吗?
PS:在某些情况下我需要它,因为例如:
i586-mingw32msvc-g++ -o outputFile `pkg-config --cflags --libs gtkmm-2.4` a.cpp b.cpp c.cpp
不起作用,但这个工作正常:
i586-mingw32msvc-g++ a.cpp b.cpp c.cpp -o outputFile `pkg-config --cflags --libs gtkmm-2.4`
example use of xargs
application in Unix can be something like this:
ls | xargs echo
which is the same as (let's say I have someFile
and someDir/
in the working directory):
echo someFile someDir
so xargs
take its input and place it at the end of the next command (here at the end of echo).
But sometimes I want xargs
to place its input somewhere in the middle of next command.
For example:
find . -type f -name "*.cpp" -print | xargs g++ -o outputFile
so if I had in the current directory files a.cpp
, b.cpp
, c.cpp
the output would be the same as with the command:
g++ -o outputFile a.cpp b.cpp c.cpp
but I want to have something like this:
g++ a.cpp b.cpp c.cpp -o outputFile
Is there a way to do it?
P.S.: I need it in some cases, because e.g.:
i586-mingw32msvc-g++ -o outputFile `pkg-config --cflags --libs gtkmm-2.4` a.cpp b.cpp c.cpp
doesn't work but this one works fine:
i586-mingw32msvc-g++ a.cpp b.cpp c.cpp -o outputFile `pkg-config --cflags --libs gtkmm-2.4`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
回答标题中提出的原始问题,即如何使用位于中间而不是末尾的输入的
xargs
:这会将命令中的
{}
替换为管道输出。 BSD 和 GNU xargs 之间存在一些细微差别,如下所述:BSD xargs(例如,在 MacOS/Darwin、freebsd 上)
使用
-I REPLACE
,这将替换字符串REPLACE
(或您在命令中传递的任何内容)。例如:手册页描述了该选项:
GNU xargs(例如在Linux上)
使用
-I REPLACE
或-i
参数,其中 手册页 描述了:-L
表示它将在单独的命令中执行每个输入:-I
上的 1To answer the original question asked in the title of how to use
xargs
with the input in the middle rather than the end:This replaces
{}
in the command with the piped output. There are some subtle differences between BSD and GNU xargs described below:BSD xargs (e.g. on MacOS/Darwin, freebsd)
Use
-I REPLACE
, which will replace the stringREPLACE
(or whatever you pass) in the command. For example:The man page describes the option:
GNU xargs (e.g. on Linux)
Use either the
-I REPLACE
or the the-i
argument, which the man page describes:The
-L 1
on-I
means that it will execute each of the input in a separate command:如果您的 xargs 版本不包含
-I
功能,另一种方法是编写一个包含您要执行的命令的小 shell 脚本:然后使用 xargs 运行该脚本:
If your version of xargs doesn't include the
-I
feature, an alternative is to write a little shell script containing the command you want to execute:Then use xargs to run that:
为此,您不需要 xargs。只需使用:
You do not need
xargs
for this. Just use:GNU Parallel http://www.gnu.org/software/parallel/ 将是解决方案也是:
*.cpp 必须适合单行 (~128 KB)。
GNU Parallel http://www.gnu.org/software/parallel/ would be a solution too:
The *.cpp must fit in a single line (~128 KB).