将 xargs 输入重定向为标准输入流而不是参数

问题描述 投票:0回答:2

有时编写以

xargs -I {} 'echo {} | [rest of command]'
开头的 xargs 命令很有用,以便将参数重定向为管道。

但是,对于较大的争论,你会遇到

xargs: argument line too long

如何告诉 xargs 直接重定向到

[rest of command]
的操作系统输入管道,以便在使用大参数时避免上述问题?

bash sh xargs
2个回答
0
投票

只需将

xargs
替换为
while read -r line; ...;done
循环即可:

find . |while read -r filename; do echo "$filename" | [rest of command]

0
投票

你所做的任何事情都不需要 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 传输到命令行参数。如果这不是您想要实现的目标,那么它就是错误的工具。

© www.soinside.com 2019 - 2024. All rights reserved.