xargs:如何通过输入参数 "describe" 输出

xargs: how to "describe" output by input arguments

我有以下 xargs 命令行:

$ gethosts |xargs -L1 -I% rx % some-command

其中gethosts是获取主机地址列表的脚本,rx是在远程主机上执行特定命令的脚本。输出可能是:

output 1
output 2
...

我想要的是命令可以输出它的参数:

host1,output1
host2,output2
...

即,我想知道,对于每个输出行,哪个主机生成了该输出。

为此,您必须使用 xargs "-c" 和 运行 sh 中的选项命令来指定用于此命令的解释器。

要打印主机(不带换行符),将使用 printf 命令。

最终结果如下命令行所示:

gethost|xargs -L1 -I% sh -c 'printf "%, " && rx % some-command'