将多个参数传递给自定义位置 xargs 中的单个命令

问题描述 投票:0回答:1

我想执行一个命令,其中参数作为单个字符串,但它们需要位于自定义位置。例如。在下面的示例中,我希望参数位于我在 cp comamnd 之后编写的任何固定参数之前,即路径参数

/somedir

我想要:

echo -n 'file1 file2 file3' | xargs -d ' ' -I{} cp {} /somedir

表现得像:

cp file1 file2 file3 /somedir

但事实并非如此...

看起来,当包含

-I{}
选项时,
-d
选项使
xargs
的行为有所不同。看起来它试图将每个参数传递给一个单独的命令调用,而不是将所有参数传递给一个单独的命令调用。

例如,这有效:

echo -n 'file1 file2' | xargs -d' ' diff

但这失败了

echo -n 'file1 file2' | xargs -d' ' -I{} diff {}

有错误:

diff: missing operand after 'file1'
diff: Try 'diff --help' for more information.
diff: missing operand after 'file2'
diff: Try 'diff --help' for more information.

我怎样才能让

echo -n 'file1 file2 file3' | xargs -d ' ' -I{} cp {} /somedir
按预期行事

linux bash shell xargs
1个回答
0
投票

-d ' '
'file1 file2 file3'
将被分成 3 个参数。

您可以使用

-d '\n'

$ echo 'file1 file2 file3' | xargs -d '\n' -I{} echo cp {} /somedir
cp file1 file2 file3 /somedir
© www.soinside.com 2019 - 2024. All rights reserved.