tags:

views:

63

answers:

2

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

+3  A: 

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.

dpk
+1  A: 
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.

Shawn J. Goff
Thanks - that's well beyond my current `sed`-fu. Sadly, this seems to not play well with interactive mode - i.e. it makes bash think that my interactive shell is noninteractive so, for instance, `less` turns into `cat`. Not sure if there's a quick fix for that, but I've tried to spoof interactive mode in the past and never had success...
Steve