Search code examples
linuxbashopenstackxargs

Xargs with command that has multiple parameters


I was advised to used these commands to get list of shutdown instances in Openstack and send them to start command as parameters:

nova list | grep SHUTOFF | cut '-d|' -f3 | xargs nova start

But it leads to error:

error: unrecognized arguments: shutdowninstance-2

If I use other commands with Xargs the the list is correct:

nova list | grep SHUTOFF | cut '-d|' -f3 | xargs echo
shutdowninstance-1 shutdowninstances-2

So first commands must be OK and the problem should be in the last part of command. I guess that the reason it because the last command has parameter start next to it. Syntax expected by Nova is nova start nameofinstance.

I studied many other questions about using Xargs here but could not find solution to this.

How should the command be changed to make it work?

EDIT: Using xargs -t gives this output:

nova start shutdowninstance-1 shutdowninstances-2

So the problem is probably that nova start accepts only one instance name at the time.

Can command given to me be adjusted to give only one parameter at the time?


Solution

  • You can use the -I option for xargs:

    nova list | grep SHUTOFF | cut '-d|' -f3 | xargs -I '{}' bash -c 'nova start {}'
    

    Alternatively you can loop over the results:

    for i in $(nova list | grep SHUTOFF | cut '-d|' -f3); do nova start $i; done