How to display the output of a Linux command on stdout and also pipe it to another command? [duplicate]

二次信任 提交于 2019-12-23 02:52:12

问题


Possible Duplicate:
How to pipe stdout while keeping it on screen ? (and not to a output file)

For example I want to run the command:

ls -l

Then I have the output to stdout:

drwxr-xr-x 2 user user 4096 Apr 12 12:34 Desktop
-rw-rw-r-- 1 user user 1234 Apr 12 00:00 file

And I want to redirect this output to another command for some further processing (like redirecting to 'head -1' to extract first line). Can I do it in just one line?


回答1:


Yes, tee will work. Something like:

ls -l | tee | head -1

To append the output to a file:

ls -l | tee -a output.txt



回答2:


There's a tool called tpipe which allows you to pipe a command to two other commands (like a fork), but it's not installed by default on most machines. Using it, your problem would be solved by:

ls -l | tpipe head -1


来源:https://stackoverflow.com/questions/10116358/how-to-display-the-output-of-a-linux-command-on-stdout-and-also-pipe-it-to-anoth

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