追加一个命令的输出到另一个输出一个命令

追加一个命令的输出到另一个输出一个命令

本文介绍了追加一个命令的输出到另一个输出一个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法给一个命令的标准输出输出组合输出到另一个命令追加到另一个的管?我用下面的方法(以 ACK-的grep 为例)

Is there a way to append the stdout output of one command to another's and pipe the combined output to another command? I used to use the following approach(taking ack-grep as an example)

# List all python, js files in different directories
ack-grep -f --py apps/ > temp
ack-grep -f --js -f media/js >> temp
cat temp | xargs somecommand

有没有办法在一个命令做到这一点?

Is there a way to do this in a single command?

推荐答案

只要运行两个 ACK-的grep 作为一个复合命令命令;然后通过管道将复方命令的结果。在男人庆典定义的第一个复合命令是括号:

Just run the two ack-grep commands as a compound command; then pipe the results of the compund command. The first compound command defined in man bash is the parentheses:

   (list) list is executed in a subshell environment (see  COMMAND  EXECU-
              TION  ENVIRONMENT below).  Variable assignments and builtin com-
              mands that affect the  shell's  environment  do  not  remain  in
              effect  after  the  command completes.  The return status is the
              exit status of list.

所以:

james@bodacious-wired:tmp$echo one > one.txt
james@bodacious-wired:tmp$echo two > two.txt
james@bodacious-wired:tmp$(cat one.txt; cat two.txt) | xargs echo
one two

您可以用花括号类似的效果,但大括号有一些语法差异(他们更挑剔需要支架和其他单词之间有空格,例如)。最大的区别是,在大括号中的命令在电流的shell环境的运行,这样他们就可以在你的环境的影响。例如:

You can use curly braces to similar effect, but curly braces have a few syntactical differences (they're more finicky about needing spaces between the brace and other words, for instance). The biggest difference is that the commands inside braces are run in the current shell environment, so they can impact on your environment. For instance:

james@bodacious-wired:tmp$HELLO=world; (HELLO=MyFriend); echo $HELLO
world
james@bodacious-wired:tmp$HELLO=world; { HELLO=MyFriend; }; echo $HELLO
MyFriend

如果你想获得真正看中你可以定义一个函数并执行:

If you want to get really fancy you can define a function and execute that:

james@bodacious-wired:tmp$myfunc () (
> cat one.txt
> cat two.txt
> )
james@bodacious-wired:tmp$myfunc | xargs echo
one two
james@bodacious-wired:tmp$

这篇关于追加一个命令的输出到另一个输出一个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 11:17