15

在 Unix 中使用xargs应用程序的示例可以是这样的:

ls | xargs echo

这与(假设我有someFile并且someDir/在工作目录中)相同:

echo someFile someDir

所以xargs把它的输入放在下一个命令的末尾(这里在 echo 的末尾)。

但有时我想把xargs它的输入放在下一个命令中间的某个地方。

例如:

find . -type f -name "*.cpp" -print | xargs g++ -o outputFile

因此,如果我在当前目录中有 files a.cppb.cppc.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`
4

4 回答 4

14

要回答标题中关于如何使用xargs中间而不是结尾的输入的原始问题:

$ echo a b c | xargs -I {} echo before {} after
before a b c after

这将{}在命令中替换为管道输出。BSD 和 GNU xargs 之间存在一些细微差别,如下所述:

BSD xargs(例如在 MacOS/Darwin、freebsd 上)

使用-I REPLACE,它将替换REPLACE命令中的字符串(或您传递的任何内容)。例如:

$ echo a b c | xargs -I {} echo before {} after
before a b c after
$ echo a b c | xargs -I REPLACE echo before REPLACE after
before a b c after
$ echo 'a
> b
> c' | xargs -L1 -I {} echo before {} after
before a after
before b after
before c after

手册页描述了该选项:

 -I replstr
     Execute utility for each input line, replacing one or more occur-
     rences of replstr in up to replacements (or 5 if no -R flag is
     specified) arguments to utility with the entire line of input.
     The resulting arguments, after replacement is done, will not be
     allowed to grow beyond replsize (or 255 if no -S flag is speci-
     fied) bytes; this is implemented by concatenating as much of the
     argument containing replstr as possible, to the constructed argu-
     ments to utility, up to replsize bytes.  The size limit does not
     apply to arguments to utility which do not contain replstr, and
     furthermore, no replacement will be done on utility itself.
     Implies -x.

GNU xargs(例如在 Linux 上)

$ echo a b c | xargs -i echo before {} after
before a b c after
$ echo a b c | xargs -I THING echo before THING after
before a b c after

使用手册页描述的 the-I REPLACE或 the-i参数:

   -I replace-str
          Replace occurrences of replace-str in the initial-arguments
          with names read from standard input.  Also, unquoted blanks do
          not terminate input items; instead the separator is the
          newline character.  Implies -x and -L 1.

   -i[replace-str], --replace[=replace-str]
          This option is a synonym for -Ireplace-str if replace-str is
          specified.  If the replace-str argument is missing, the effect
          is the same as -I{}.  This option is deprecated; use -I
          instead.

-L 1on-I表示它将在单独的命令中执行每个输入:

$ echo "a
> b
> c" | xargs -I THING echo before THING after
before a after
before b after
before c after

-i没有这种效果,但显然已被弃用。)

于 2017-01-13T05:29:28.483 回答
6

如果您的 xargs 版本不包含该-I功能,另一种方法是编写一个包含您要执行的命令的小 shell 脚本:

#!/bin/sh
exec i586-mingw32msvc-g++ "$@" -o outputFile...

然后使用 xargs 运行它:

find . -type f -name "*.cpp" -print | xargs my_gcc_script
于 2009-09-05T12:27:28.600 回答
1

你不需要xargs这个。只需使用:

g++ `find . -type f -name '*.cpp'` -o outputFile
于 2009-09-05T17:05:45.143 回答
1

GNU Parallel http://www.gnu.org/software/parallel/也是一个解决方案:

find . -type f -name "*.cpp" -print | parallel -Xj1 g++ {} -o outputFile

*.cpp 必须适合单行 (~128 KB)。

于 2010-06-10T19:10:36.857 回答