bash 如何将带参数的命令传递给 xargs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34669239/
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
How to pass command with parameters to xargs
提问by Sato
echo ls -l -a / | xargs sh -c
echo ls -l -a / | xargs sh -c
How to make above command work?
如何使上述命令工作?
it only list current directory
它只列出当前目录
seems only ls is passed to xargs
似乎只有 ls 传递给 xargs
echo '"ls -l -a /"' | xargs sh -c
would work though, but the input I got has no ""
echo '"ls -l -a /"' | xargs sh -c
虽然会工作,但我得到的输入没有“”
采纳答案by darklion
The -c
flag to sh
only accepts one argument while xargs is splitting the arguments on whitespace - that's why the double quoting works (one level to make it a single word for the shell, one for xargs).
在-c
以标志sh
只接受一个参数,而xargs的是分割上空白的论点-这就是为什么双引号作品(一个水平,使之成为一个字的外壳,一个xargs的)。
If you use the -0
or null
argument to xargs
your particular case will work:
如果您在特定情况下使用-0
ornull
参数xargs
将起作用:
echo ls -l -a / | xargs -0 sh -c
回答by dhirajforyou
Source: http://www.unixmantra.com/2013/12/xargs-all-in-one-tutorial-guide.html
来源:http: //www.unixmantra.com/2013/12/xargs-all-in-one-tutorial-guide.html
as mentioned by @darklion, -Idenoted the argument list marker and -cdenotes bash command to run on every input line to xargs.
正如@darklion 所提到的,-I表示参数列表标记,-c表示要在 xargs 的每个输入行上运行的 bash 命令。
Simply print the input to xargs:
只需将输入打印到 xargs:
ls -d */ | xargs echo
#One at a time
ls -d */ | xargs -n1 echo
More operations on every input:
每个输入的更多操作:
ls -d */ | xargs -n1 -I {} /bin/bash -c ' echo {}; ls -l {}; '
You can replace {}with customized string as:
您可以将{}替换为自定义字符串:
ls -d */ | xargs -n1 -I file /bin/bash -c ' echo file; ls -l file; '