例如,使用命令
cat foo.txt | xargs -I{} -n 1 -P 1 sh -c "echo {} | echo"
foo.txt包含两行
foo bar
上述命令什么都不打印。
cat foo.txt | xargs -J % -n 1 sh -c "echo % | bar.sh"
棘手的部分是,xargs执行隐式的子shell调用。 这里sh显式调用,pipe不成为父传送器的一部分
如果你想处理foo.txt的所有行,你将不得不使用一个循环。 使用&将过程放到后台
while read line; do echo $line | bar.sh & done < foo.txt
如果您的input包含空格,则将内部字段分隔符临时设置为换行符
# save the field separator OLD_IFS=$IFS # new field separator, the end of line IFS=$'\n' for line in $(cat foo.txt) ; do echo $line | bar.sh & done # restore default field separator IFS=$OLD_IFS