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

8 unix g++ xargs

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

ls | xargs echo
Run Code Online (Sandbox Code Playgroud)

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

echo someFile someDir
Run Code Online (Sandbox Code Playgroud)

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

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

例如:

find . -type f -name "*.cpp" -print | xargs g++ -o outputFile
Run Code Online (Sandbox Code Playgroud)

所以如果我在当前目录中的文件有a.cpp,b.cpp,c.cpp输出将是相同的用命令:

g++ -o outputFile a.cpp b.cpp c.cpp
Run Code Online (Sandbox Code Playgroud)

但是我想要这样的东西:

g++ a.cpp b.cpp c.cpp -o outputFile
Run Code Online (Sandbox Code Playgroud)

有办法吗?

PS:在某些情况下我需要它,因为例如:

i586-mingw32msvc-g++ -o outputFile `pkg-config --cflags --libs gtkmm-2.4` a.cpp b.cpp c.cpp
Run Code Online (Sandbox Code Playgroud)

不起作用,但这个工作正常:

i586-mingw32msvc-g++ a.cpp b.cpp c.cpp -o outputFile `pkg-config --cflags --libs gtkmm-2.4`
Run Code Online (Sandbox Code Playgroud)

Rec*_*nic 9

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

$ echo a b c | xargs -I {} echo before {} after
before a b c after
Run Code Online (Sandbox Code Playgroud)

这将{}在命令中替换管道输出.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
Run Code Online (Sandbox Code Playgroud)

手册页描述的选项:

 -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.
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

使用手册页描述的-I REPLACE-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.
Run Code Online (Sandbox Code Playgroud)

-L 1-I意味着它将在一个单独的命令执行每个输入:

$ echo "a
> b
> c" | xargs -I THING echo before THING after
before a after
before b after
before c after
Run Code Online (Sandbox Code Playgroud)

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


Ken*_*ter 6

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

#!/bin/sh
exec i586-mingw32msvc-g++ "$@" -o outputFile...
Run Code Online (Sandbox Code Playgroud)

然后使用xargs运行:

find . -type f -name "*.cpp" -print | xargs my_gcc_script
Run Code Online (Sandbox Code Playgroud)