如何使Bash自动将每个命令的输出传输到tee之类的东西?

时间:2020-12-13 01:46:46

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

我在$PROMPT_COMMAND中使用了一些技巧来自动保存我运行到数据库的每个命令:

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 saved to a variable (or even a file) - is there any way to do this?

save_command是一个更复杂的函数。就好了保存的头/尾每个命令的输出,但是我想不出一个合理的方法,除了手动将某种外壳函数我所有类型(这变得更加痛苦和复杂的管道或布尔表达式)。基本上,我只是想让/dev/tty的前10行和最后10行保存到一个变量(甚至是一个文件)——有什么方法可以做到这一点吗?

2 个解决方案

#1


2  

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.

脚本(1)可能会启动。它不会让你只记录前10行和后10行,但是你可以对它的输出做一些后处理。

#2


2  

bash | tee /dev/tty ./bashout

This saves all stdout gets saved to bashout.

这将保存所有stdout以攻击。

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

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

每个命令的尾部都会被写入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.

每个命令的前10行和最后10行stdout都被编写为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.

这些不会保存命令,但是如果您修改save_command以将命令打印到stdout,它就会到达那里。

#1


2  

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.

脚本(1)可能会启动。它不会让你只记录前10行和后10行,但是你可以对它的输出做一些后处理。

#2


2  

bash | tee /dev/tty ./bashout

This saves all stdout gets saved to bashout.

这将保存所有stdout以攻击。

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

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

每个命令的尾部都会被写入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.

每个命令的前10行和最后10行stdout都被编写为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.

这些不会保存命令,但是如果您修改save_command以将命令打印到stdout,它就会到达那里。