Help - Search - Members - Calendar
Full Version: Capturing Interactive Shell Script Output
OESF Forums > General Forums > Desktop Operating Systems Issues > Linux Issues
daniel3000
Hi guys,

does anyone know how to capture the output (stdout AND stderr) of an interactive shell script to a file?

Of course I could write a "| tee ..." behind every single command, but I'd like to avoid that hassle.
Using "| tee" on the entire script eats the interactivity ("read -p" output isn't shown on the screen anymore).

Thank you!
daniel
Da_Blitz
i belive there is a pam module, however it might not do what you want

i know there is a program out there but its not perfect, you could hack up some messy code using tee as you said by using pipes and file descripter redirection however perhaps bash has an option to allow you to dump IO

well lets see what dirty code i can come up with

CODE
#!/bin/sh

# use these for real time logging and processing (ie external program to add timestamps)
#mkfifo stdin
#mkfifo stdout

tee stdin | $* 2>&1 | tee stdout


just watch out for stuff that uses the ncurses libary, this will not work with them. same goes for anything using escape chars, or if it does use it make sure you only playback the script on the same term (otherwise escape chars might be diffrent)
grog
The script utility saves all command line output. If it's not available for your ROM, it's part of the util-linux kernel package if you want to compile it. HTH
daniel3000
QUOTE(grog @ Apr 10 2007, 04:55 PM)
The script utility saves all command line output. If it's not available for your ROM, it's part of the util-linux kernel package if you want to compile it. HTH
*


hmm- the util-linux package on http://www.tyrannozaurus.com/feed/beta3/feed/ does not contain "script".

Now trying to compile the sources 2.12r.
Thanks for the hint! It just looks like what I need!

daniel
grog
QUOTE(daniel3000 @ Apr 10 2007, 10:59 AM)
Now trying to compile the sources 2.12r.
Thanks for the hint! It just looks like what I need!
*

Glad I could help. Don't forget to share smile.gif
daniel3000
QUOTE(grog @ Apr 10 2007, 10:01 PM)
QUOTE(daniel3000 @ Apr 10 2007, 10:59 AM)
Now trying to compile the sources 2.12r.
Thanks for the hint! It just looks like what I need!
*

Glad I could help. Don't forget to share smile.gif
*



Of course I won't! smile.gif

So here is the solution:
Attached is the script utility (stripped binary only, compiled for pdaXrom 1.1.0beta3), just unzip and copy it to /usr/bin).

I have also developed a nice trick to make usage of "script" totally transparent to the user, so the user still only has to call the shell script as usual.
Normally, in order to create a log from a shell script execution, you need to call the script with "script" this way:

CODE
script -c "shellscript.sh" logfilename


This can be automated within the actual shell script to be executed and logged.
Just put these lines at the beginning of the shell script:

CODE
TODAY=`date +%Y-%m-%d_%Hh%M`

if [ ! "$1" = "run" ]; then
 script -c "$0 run" ${TODAY}_LOGFILE
 echo Created logfile ${TODAY}_LOGFILE
 exit 0
fi


So when calling the shell script as usual, it actually calls "script" which, with the -c parameter, calls the shell script recursively once more to execute its actual contents.
The "run" parameter is used to determine if the user calls the script (go into the "if") or if script has called it (run the actual script contents).

In the above example, "script" will create a logfile in the currect directory with the name (for example)

2007-04-11_09:25_LOGFILE

Interactivity of the shell script, here using mainly echo and read, is fully preserved.

Thank you for your help!

daniel
ShiroiKuma
My preferred solution to this problem is to use Emacs.

When you run the shell in Emacs, either bash or eshell, you can then simply save the buffer to a file, you can browse the shell window like a text file etc.

It's superb...
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2014 Invision Power Services, Inc.