`tee` command equivalent for *input*?

2019-08-02 04:38发布

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)

3条回答
我想做一个坏孩纸
2楼-- · 2019-08-02 05:04

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楼-- · 2019-08-02 05:10

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.

查看更多
你好瞎i
4楼-- · 2019-08-02 05:31

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)

查看更多
登录 后发表回答