替换 osx 中的 xargs -d

Cha*_*ert 35 xargs macos

在 Linux 中,您可以使用以下顺序对四个不同的服务器xargs -d,快速运行hostname命令:

echo -n 1,2,3,4 |xargs -d, -I{} ssh root@www{}.example.com hostname

看起来 OSX xargs 命令不支持分隔符参数。您可以使用不同格式的echo或通过其他一些命令行实用程序获得相同的结果吗?

slh*_*hck 57

或者,您始终可以xargs通过Homebrew和 GNUfindutils包安装 GNU 。

安装 Homebrew:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Run Code Online (Sandbox Code Playgroud)

按照说明操作。然后安装 findutils:

brew install findutils
Run Code Online (Sandbox Code Playgroud)

这将为您提供 GNU xargsas gxargs,并且您可以使用您在 GNU/Linux 中习惯的语法。findutils 包中的其他基本命令也是如此,例如gfindorglocategupdatedb,它们在 OS X 上具有不同的 BSD 对应项。


Gor*_*son 17

怎么样:

echo {1..4} | xargs -n1 -I{} ssh root@www{}.example.com hostname
Run Code Online (Sandbox Code Playgroud)

来自man xargs

-n number
Set the maximum number of arguments taken from standard input for each invocation of utility.
Run Code Online (Sandbox Code Playgroud)

  • 如果值可以包含空格、换行符或制表符,则可以使用类似 `printf 'a\0a' | xargs -0 -n1 回显`。 (2认同)