`tee` command equivalent for *input*?

元气小坏坏 提交于 2019-12-11 10:46:58

问题


The unix tee command splits the standard input to stdout AND a file.

What I need is something that works the other way around, merging several inputs to one output - I need to concatenate the stdout of two (or more) commands.
Not sure what the semantics of this app should be - let's suppose each argument is a complete command.

Example:

>  eet "echo 1" "echo 2" > file.txt

should generate a file that has contents

1
2

I tried

>  echo 1 && echo 2 > zz.txt

It doesn't work.

Side note: I know I could just append the outputs of each command to the file, but I want to do this in one go (actually, I want to pipe the merged outputs to another program).
Also, I could roll my own, but I'm lazy whenever I can afford it :-)

Oh yeah, and it would be nice if it worked in Windows (although I guess any bash/linux-flavored solution works, via UnxUtils/msys/etc)


回答1:


Try

( echo 1; echo 2 ) > file.txt

That spawn a subshell and executes the commands there

{ echo 1; echo 2; } > file.txt

is possible, too. That does not spawn a subshell (the semicolon after the last command is important)




回答2:


I guess what you want is to run both commands in parallel, and pipe both outputs merged to another command.

I would do:

( echo 1 & echo 2 ) | cat

Where "echo 1" and "echo 2" are the commands generating the outputs and "cat" is the command that will receive the merged output.




回答3:


echo 1 > zz.txt && echo 2 >> zz.txt

That should work. All you're really doing is running two commands after each other, where the first redirects to a file, and then, if that was successful, you run another command that appends its output to the end of the file you wrote in the first place.



来源:https://stackoverflow.com/questions/590548/tee-command-equivalent-for-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!