在一行中将参数传递给多个命令

Zai*_*aid 11 command-line

我希望能够在一行中对同一个文件运行多个命令。我目前这样做的方式是:

commandA file && commandB file && perl -ne '...' file
Run Code Online (Sandbox Code Playgroud)

我的直觉告诉我,应该有一种方法只提供一次文件名参数,然后通过管道xargs或类似的方式将它同时传递给两个命令:

find file | xargs commandA && xargs commandB && xargs perl -ne '...'
Run Code Online (Sandbox Code Playgroud)

当我尝试这个时,只有第一个命令运行。我怎样才能实现我想做的事?

max*_*zig 14

您可以为此定义一个局部变量:

f=file; commandA $f && commandB $f && ...
Run Code Online (Sandbox Code Playgroud)

您还可以无条件地(替换&&;)或并行(替换&&&)执行所有操作。

或者,您也可以使用 shell 历史扩展来引用以前的参数:

commandA file && commandB !:1 && ...
Run Code Online (Sandbox Code Playgroud)


Den*_*son 9

对于具有进程替换的 Bash、Korn 和 Z 等 shell,您可以执行以下操作:

find file | tee >(xargs commandA) >(xargs commandB) >(xargs perl -ne '...')
Run Code Online (Sandbox Code Playgroud)