bash 带有多个参数的命令的 Xargs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27267260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xargs with command that has multiple parameters
提问by Madoc Comadrin
I was advised to used these commands to get list of shutdown instances in Openstack and send them to start command as parameters:
我被建议使用这些命令来获取 Openstack 中的关闭实例列表,并将它们作为参数发送到启动命令:
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:
如果我将其他命令与 Xargs 一起使用,则列表是正确的:
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
.
所以第一条命令必须没问题,问题应该出在命令的最后一部分。我猜这是因为最后一个命令start
旁边有参数。Nova 预期的语法是nova start nameofinstance
.
I studied many other questions about using Xargs here but could not find solution to this.
我在这里研究了许多有关使用 Xargs 的其他问题,但找不到解决方案。
How should the command be changed to make it work?
应该如何更改命令以使其工作?
EDIT: Using xargs -t
gives this output:
编辑:使用xargs -t
给出了这个输出:
nova start shutdowninstance-1 shutdowninstances-2
So the problem is probably that nova start
accepts only one instance name at the time.
所以问题可能是当时nova start
只接受一个实例名称。
Can command given to me be adjusted to give only one parameter at the time?
给我的命令可以调整为一次只给出一个参数吗?
采纳答案by Josh Jolly
You can use the -I
option for xargs
:
您可以将该-I
选项用于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