开发者

How can I make Bash automatically pipe the output of every command to something like tee?

开发者 https://www.devze.com 2023-01-20 12:03 出处:网络
I use some magic in $PROMPT_COMMAND to automatically save every command I run to a database: PROMPT_COMMAND=\'save_command \"$(history 1)\"\'

I use some magic in $PROMPT_COMMAND to automatically save every command I run to a database:

PROMPT_COMMAND='save_command "$(history 1)"'

where save_command is a more complicated function. It would be nice to save also the head/tail of the output of each command, but I can't think of a reasonable way to do this, other than manually prepending some sort of shell function to everything I type (and this becomes even more painful with complicated pipelines or boolean expressions). Basically, I just want the first and last 10 lines of whatever went to /dev/tty to get sa开发者_运维百科ved to a variable (or even a file) - is there any way to do this?


script(1) will probably get you started. It won't let you just record the first and last 10 lines, but you can do some post-processing on its output.


bash | tee /dev/tty ./bashout

This saves all stdout gets saved to bashout.

bash | tee /dev/tty | tail > ./bashout

The tail of stdout of every command gets written to bashout.

bash | tee /dev/tty | sed -e :a -e '10p;$q;N;11,$D;ba' > ./bashout

The first and last 10 lines of stdout of every command gets written to bashout.

These don't save the command, but if you modify your save_command to print the command to stdout, it will get in there.

0

精彩评论

暂无评论...
验证码 换一张
取 消