有时编写以
xargs -I {} 'echo {} | [rest of command]'
开头的 xargs 命令很有用,以便将参数重定向为管道。
但是,对于较大的争论,你会遇到
xargs: argument line too long
。
如何告诉 xargs 直接重定向到
[rest of command]
的操作系统输入管道,以便在使用大参数时避免上述问题?
只需将
xargs
替换为 while read -r line; ...;done
循环即可:
find . |while read -r filename; do echo "$filename" | [rest of command]
你所做的任何事情都不需要 xargs,无论在哪里。
#!/usr/bin/env bash
for ((i=0; i<10; i++)); do
openssl rand -base64 21000000 | tr -d '\n'
echo
done >out.b64
while IFS= read -r line; do
{ rest-of-command; } <<<"$line"
done <out.b64
xargs 是一个用途有限的工具:它将内容从 stdin 传输到命令行参数。如果这不是您想要实现的目标,那么它就是错误的工具。