cat a.txt | xargs -I % echo %

在上面的例子中,xargs使用echo %作为命令参数。但在某些情况下,我需要多个命令来处理参数,而不是一个。例如:

cat a.txt | xargs -I % {command1; command2; ... }

但是xargs不接受这种形式。我知道的一种解决方案是,我可以定义一个函数来包装命令,但我想避免这样做,因为它很复杂。有没有更好的解决方案?


当前回答

这是另一种没有xargs和cat的方法:

while read stuff; do
  command1 "$stuff"
  command2 "$stuff"
  ...
done < a.txt

其他回答

你可以使用

cat file.txt | xargs -i  sh -c 'command {} | command2 {} && command3 {}'

{} =变量为文本文件中的每一行

cat a.txt | xargs -d $'\n' sh -c 'for arg do command1 "$arg"; command2 "$arg"; ...; done' _

...或者,不用无用地使用cat:

<a.txt xargs -d $'\n' sh -c 'for arg do command1 "$arg"; command2 "$arg"; ...; done' _

来解释一些细节:

The use of "$arg" instead of % (and the absence of -I in the xargs command line) is for security reasons: Passing data on sh's command-line argument list instead of substituting it into code prevents content that data might contain (such as $(rm -rf ~), to take a particularly malicious example) from being executed as code. Similarly, the use of -d $'\n' is a GNU extension which causes xargs to treat each line of the input file as a separate data item. Either this or -0 (which expects NULs instead of newlines) is necessary to prevent xargs from trying to apply shell-like (but not quite shell-compatible) parsing to the stream it reads. (If you don't have GNU xargs, you can use tr '\n' '\0' <a.txt | xargs -0 ... to get line-oriented reading without -d). The _ is a placeholder for $0, such that other data values added by xargs become $1 and onward, which happens to be the default set of values a for loop iterates over.

我更喜欢允许试运行模式的样式(没有| sh):

cat a.txt | xargs -I % echo "command1; command2; ... " | sh

管道也适用:

cat a.txt | xargs -I % echo "echo % | cat " | sh

这是另一种没有xargs和cat的方法:

while read stuff; do
  command1 "$stuff"
  command2 "$stuff"
  ...
done < a.txt

我做的一件事是添加到.bashrc/。配置此功能:

function each() {
    while read line; do
        for f in "$@"; do
            $f $line
        done
    done
}

然后你就可以做

... | each command1 command2 "command3 has spaces"

它比xargs或-exec更简洁。如果还需要这种行为,还可以修改该函数,将读取的值插入到每个命令中的任意位置。