Shell Tool Integration
Xargs Batch
Group Arguments
Some tools accept many arguments at once. xargs -n groups input words into batches before running the command.
Program
Play the script to choose the batch size and see how the arguments are grouped.
xargs_batch_plan.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
batch_size=2
items=("api" "worker" "web")
command="xargs -n $batch_size restart"
if [ "$batch_size" -eq 2 ]; then
groups="api worker|web"
else
groups="api worker web"
fi
echo "$command -> $groups"
#!/usr/bin/env bash
batch_size=3
items=("api" "worker" "web")
command="xargs -n $batch_size restart"
if [ "$batch_size" -eq 2 ]; then
groups="api worker|web"
else
groups="api worker web"
fi
echo "$command -> $groups"
batch_size ← 2
3batch_size=24items=("api" "worker" "web")values this step2batch_sizeitems ← api worker web
3batch_size=24items=("api" "worker" "web")5command="xargs -n $batch_size restart"values this stepapi worker webitemscommand ← xargs -n 2 restart
4items=("api" "worker" "web")5command="xargs -n $batch_size restart"6if [ "$batch_size" -eq 2 ]; thenvalues this stepxargs -n 2 restartcommand2batch_sizeif [ "$batch_size" -eq 2 ]; then
5command="xargs -n $batch_size restart"6if [ "$batch_size" -eq 2 ]; then7 groups="api worker|web"values this step2batch_sizegroups ← api worker|web
6if [ "$batch_size" -eq 2 ]; then7 groups="api worker|web"8elsevalues this stepapi worker|webgroupsecho "$command -> $groups"
10fi11echo "$command -> $groups"outputxargs -n 2 restart -> api worker|webvalues this stepxargs -n 2 restartcommandapi worker|webgroups
batch_size ← 3
3batch_size=34items=("api" "worker" "web")values this step3batch_sizeitems ← api worker web
3batch_size=34items=("api" "worker" "web")5command="xargs -n $batch_size restart"values this stepapi worker webitemscommand ← xargs -n 3 restart
4items=("api" "worker" "web")5command="xargs -n $batch_size restart"6if [ "$batch_size" -eq 2 ]; thenvalues this stepxargs -n 3 restartcommand3batch_sizeif [ "$batch_size" -eq 2 ]; then
5command="xargs -n $batch_size restart"6if [ "$batch_size" -eq 2 ]; then7 groups="api worker|web"values this step3batch_sizegroups ← api worker web
8else9 groups="api worker web"10fivalues this stepapi worker webgroupsecho "$command -> $groups"
10fi11echo "$command -> $groups"outputxargs -n 3 restart -> api worker webvalues this stepxargs -n 3 restartcommandapi worker webgroups
xargs
`xargs` converts input words into command arguments.
batch size
`-n` controls how many arguments go into each command run.
grouping
Changing the batch size changes the command grouping without changing the input list.