Casey
Casey

Reputation: 2709

How to use xargs to run command multiple times?

I have a command that can be run once like:

heroku local:run python put_in_db.py --query='ffb557'

What I want to do is take a list of queries, like ["ffb557", "ttr887"] and run the command for each query. I tried running one to start, but get an error:

echo 'ffb557' | xargs heroku local:run python put_in_db.py --query='{}'

put_in_db.py: error: unrecognized arguments: ffb557

Any idea what I'm doing wrong?

Upvotes: 2

Views: 862

Answers (1)

James Risner
James Risner

Reputation: 6076

Linux:

% ( echo ffb557 ; echo ttr887 ) | xargs -i echo heroku local:run python put_in_db.py --query='{}'
heroku local:run python put_in_db.py --query=ffb557
heroku local:run python put_in_db.py --query=ttr887

Macos & BSD:

% ( echo ffb557 ; echo ttr887 ) | xargs -I % -n 1 echo heroku local:run python put_in_db.py --query='%'
heroku local:run python put_in_db.py --query=ffb557
heroku local:run python put_in_db.py --query=ttr887

Remove the echo to run the commands.

Upvotes: 2

Related Questions