如何将 bash 命令的输出作为参数传递给另一个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11381346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:42:24  来源:igfitidea点击:

how do I pass the output of a bash command as parameters to another?

bash

提问by AriehGlazer

I have a program that returns me a list of file names. I need to pass these file's contents to another program.

我有一个程序可以返回文件名列表。我需要将这些文件的内容传递给另一个程序。

I know how to use catto pass content:

我知道如何使用cat来传递内容:

cat file1 file2 file3 | some_program

what I want to do is something like:

我想做的是:

list=`get_file_list`
cat ${list} | some_program

Though doing the above will simply pass the content of the list variable, rather than the files' content.

尽管执行上述操作只会传递列表变量的内容,而不是文件的内容。

回答by JayZee

To pass the output of,a program to another you can use xargs, for example

要将一个程序的输出传递给另一个程序,您可以使用 xargs,例如

     find . -name "myfile*" -print | xargs grep "myword" 

This one searches for files called myfile* and look for a key inside of them.

这将搜索名为 myfile* 的文件并在其中查找密钥。