-   Programming (
-   -   Log all of a shell script's I/O to the console and a file (

trevelluk 04-12-2005 11:03 AM

Log all of a shell script's I/O to the console and a file
Hi everyone, hopefully someone can help me with this.

I've got a very large (interactive) shell script, which performs various administration operations on a PostgreSQL database. It's been suggested that it might be useful for all of the console I/O in this script to be logged to a file, as well as being displayed on the console. I know I can use tee to almost achieve this, but unfortunately this is only logging the output to the file, not the user input. I know I could modify the script so that it writes the user input to the file itself, but this seems like a messy solution to me.

Does anyone know of a way that tee can be used to achieve this affect - or some other way I can do it?

Any advice will be greatly appreciated.

theYinYeti 04-12-2005 11:57 AM

I don't have time to test, but maybe something along those lines:
exec 3<&0- # move current stdin to fd 3.
exec 0< <(tee /inputLogFile 0<&3)
in the script.


trevelluk 04-12-2005 12:15 PM

Nope, I just tried that and it locked up the console. I'll have a look at what exactly's going on with those commands, and see if I can get anywhere.

trevelluk 04-14-2005 05:38 AM

Ah well, the spec for the thing's changed now anyway, so the question's academic. Thanks for your suggestion, theYinYeti. If I do find a way to do this, I'll let you know.

jlliagre 04-14-2005 06:56 AM

The "script" command seems to fit exactly to the need.

trevelluk 04-14-2005 07:21 AM


I didn't know about that one.


theYinYeti 04-18-2005 07:06 AM

Neither did I! Here at work (cygwin): "bash: script: command not found."
I'll have to try this and see what it does at home...


jlliagre 04-18-2005 07:35 AM

Here's a old posting about the issue, may be of help:

sirclif 04-18-2005 02:40 PM

you could use tee twice with the append option

tee -a logfile | script_file | tee -a logfile

this runs the script_file, tee takes all of its standard and writes it to logfile and the screen, then all standard input is first ran through tee, which writes it to logfile and then passes it off to the script_file

All times are GMT -5. The time now is 06:25 PM.