web-dev-qa-db-ja.com

OSXでのxargs -dの置き換え

Linuxでは、xargs -d,は、4つの異なるサーバーに対してhostnameコマンドを次のように連続した名前ですばやく実行します。

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

OSX xargsコマンドは区切り文字パラメーターをサポートしていないようです。異なる形式のechoを使用して、または他のコマンドラインユーティリティを使用して同じ結果を得ることができますか?

27
Chase Seibert

または、常にGNU xargs through Homebrew およびGNU findutilsパッケージをインストールすることもできます。 。

Homebrewをインストールします。

Ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

指示に従ってください。次に、findutilsをインストールします。

brew install findutils

これは、GNU xargs as gxargsを提供し、GNU/Linuxで慣れている構文を使用できます。同じことが他の基本的なgfindまたはglocateまたはgupdatedbなど、findutilsパッケージにあるコマンドで、OS Xで対応するBSDが異なります。

48
slhck

並列で実行したい場合は、GNU Parallel:

parallel ssh root@www{}.example.com hostname ::: {1..4}
2
Ole Tange

OSXでtrを使用してコンマを新しい行に変換し、xargsを使用して各項目を次のように列挙することもできます。

echo -n 1,2,3,4 | tr -s ',' '\n' | xargs -I{} ssh root@www{}.example.com hostname
1
John

printf

$ printf '%s\n' 1 2 3 4 | xargs -I{} echo ssh root@www{}.example.com hostname
ssh [email protected] hostname
ssh [email protected] hostname
ssh [email protected] hostname
ssh [email protected] hostname
0