not*_*ser 17 bash xargs shell-script
我知道,鉴于l="a b c"
,
echo $l | xargs ls
Run Code Online (Sandbox Code Playgroud)
产量
ls a b c
Run Code Online (Sandbox Code Playgroud)
哪个构造产生
mycommand -f a -f b -f c
Run Code Online (Sandbox Code Playgroud)
Sat*_*ura 18
一种方法:
echo "a b c" | xargs printf -- '-f %s\n' | xargs mycommand
Run Code Online (Sandbox Code Playgroud)
这假定a
,b
和c
不包含空格、换行符、引号或反斜杠。:)
使用 GNU,findutil
您可以处理一般情况,但稍微复杂一些:
echo -n "a|b|c" | tr \| \\0 | xargs -0 printf -- '-f\0%s\0' | xargs -0 mycommand
Run Code Online (Sandbox Code Playgroud)
您可以取代|
一些其他字符分隔符,没有出现在a
,b
或c
。
编辑:正如@MichaelMol 所指出的,如果参数列表很长,则存在溢出可以传递给mycommand
. 发生这种情况时,最后一个xargs
将拆分列表并运行 的另一个副本mycommand
,并且存在留下未终止的-f
. 如果你担心这种情况,你可以用这样xargs -0
的东西替换上面的最后一个:
... | xargs -x -0 mycommand
Run Code Online (Sandbox Code Playgroud)
这不会解决问题,但是mycommand
当参数列表太长时它会中止运行。
解决它的更好方法(IMO)是:
在zsh
:
l=(a b c)
mycommand -f$^l
Run Code Online (Sandbox Code Playgroud)
或使用数组压缩,以便参数不附加到选项:
l=(a b c) o=(-f)
mycommand "${o:^^l}"
Run Code Online (Sandbox Code Playgroud)
这样,如果l
数组包含空元素或包含空格或任何其他有问题的字符的元素,它仍然有效xargs
。例子:
$ l=(a '' '"' 'x y' c) o=(-f)
$ printf '<%s>\n' "${o:^^l}"
<-f>
<a>
<-f>
<>
<-f>
<">
<-f>
<x y>
<-f>
<c>
Run Code Online (Sandbox Code Playgroud)在rc
:
l=(a b c)
mycommand -f$l
Run Code Online (Sandbox Code Playgroud)在fish
:
set l a b c
mycommand -f$l
Run Code Online (Sandbox Code Playgroud)(AFAIK,rc
并且fish
没有数组压缩)
使用旧式的类似 Bourne 的 shell bash
,你总是可以这样做(仍然允许$@
数组元素中的任何字符):
set -- a b c
for i do set -- "$@" -f "$i"; shift; done
mycommand "$@"
Run Code Online (Sandbox Code Playgroud)