以前bashのループで回避したけど、そもそもxargs
で1個ずつ引数渡せないの?と調べたら-n
オプションがあったのでメモ。
$ kubectl config get-contexts -o name | grep ... | xargs kubectl config delete-context Delete the specified context from the kubeconfig Examples: # Delete the context for the minikube cluster kubectl config delete-context minikube Usage: kubectl config delete-context NAME [options] Use "kubectl options" for a list of global command-line options (applies to all commands).
これをやろうとしても、kubectl config delete-context
に渡せるコンテキスト名は1個だけなので、対象が複数あるとエラーになる。
そこで、-n(--max-args=)
オプションで1
を指定する。
-n, --max-args=MAX-ARGS Use at most MAX-ARGS arguments per command line
こんな感じ
$ kubectl config get-contexts -o name | grep ... | xargs -n1 kubectl config delete-context
すると、「まとめて引数をぶち込んで一括実行」じゃなく、「引数を1個ずつ渡して実行」になるので期待する動作をする。