Unix - “xargs” - 输出“在中间”(不在最后!)

时间:2009-09-05 11:57:24

标签: unix g++ xargs

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

ls | xargs echo

与(工作目录中我有someFilesomeDir/相同):

echo someFile someDir

所以xargs接受输入并将放在下一个命令的末尾(这里是echo的结尾)。

但有时我希望xargs将其输入放在的下一个命令的中间位置

例如:

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

所以,如果我在当前目录文件a.cppb.cppc.cpp中输出的内容与命令相同:

g++ -o outputFile a.cpp b.cpp c.cpp

但我希望有这样的东西:

g++ a.cpp b.cpp c.cpp -o outputFile

有办法吗?

P.S。:在某些情况下我需要它,因为例如:

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 个答案:

答案 0 :(得分:8)

要回答标题中提到的原始问题,如果在中间而不是结尾使用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

man page描述了选项:

 -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

使用-I REPLACE-i参数the man page描述:

   -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 1上的-I表示它将在单独的命令中执行每个输入:

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

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

答案 1 :(得分:6)

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

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

然后使用xargs运行:

find . -type f -name "*.cpp" -print | xargs my_gcc_script

答案 2 :(得分:2)

您不需要xargs。只需使用:

g++ `find . -type f -name '*.cpp'` -o outputFile

答案 3 :(得分:1)

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

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